/dollar-string

Helper utilities for dealing with dollar strings

Primary LanguageJavaScriptMIT LicenseMIT

Dollar String

Helper utilities for dealing with dollar strings.

NPM License

Build Status Dependencies Dev dependencies

Getting started

npm install dollar-string
var dollarString = require('dollar-string');

Usage

dollarString.valid(value)

console.log(dollarString.valid("$1.00"));  //TRUE
console.log(dollarString.valid("$2.53"));  //TRUE
console.log(dollarString.valid("$10.2"));  //TRUE
console.log(dollarString.valid("$5"));     //TRUE
console.log(dollarString.valid("1.00"));   //TRUE

console.log(dollarString.valid("$1.004")); //FALSE
console.log(dollarString.valid("$1.00c")); //FALSE
console.log(dollarString.valid("$1.a0"));  //FALSE

dollarString.toCents(value)

console.log(dollarString.toCents("$1.00")); // 100
console.log(dollarString.toCents('$2.22')); // 222
console.log(dollarString.toCents('$1.50')); // 150

dollarString.toDollars(value)

console.log(dollarString.toDollars("$1.00")); // 1
console.log(dollarString.toDollars('$2.22')); // 2.22
console.log(dollarString.toDollars('$1.50')); // 1.5

dollarString.fromCents(value)

console.log(dollarString.fromCents(100)); // '$1.00'
console.log(dollarString.fromCents(222)); // '$2.22'
console.log(dollarString.fromCents(150)); // '$1.50'

dollarString.fromDollars(value)

console.log(dollarString.fromDollars(1));    // '$1.00'
console.log(dollarString.fromDollars(2.22)); // '$2.22'
console.log(dollarString.fromDollars(1.5));  // '$1.50'

dollarString.compare(val1, val2)

returns

  • -1 if val1 is smaller than val2
  • 0 if both values are equal
  • 1 if val1 is greater than val2
console.log(dollarString.compare("$1.00", "$2.00"));  // -1
console.log(dollarString.compare("$10.00", "$10"));   // 0
console.log(dollarString.compare("$2.00" , "$1.00")); // 1

dollarString.multipleOf(val1, val2)

returns

  • true if val1 is a multiple of val2
  • false otherwise
console.log(dollarString.multipleOf("$2.00", "$1.25"));  // false
console.log(dollarString.multipleOf("$10.00", "$1"));   // true