diff --git a/README.md b/README.md index 48b9e1f..e0efc21 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ This code was originally written for [hebcal/hebcal-js](https://github.com/hebca Install gematriya from NPM, bower, or just take the `gematriya.js` script from this repo. -``` +```bash npm install gematriya bower install gematriya ``` @@ -19,14 +19,18 @@ On the client side, the API is available through the global function `gematriya` A single function is available. Pass it a `Number` or `String`. Given a number, it will return the string representation. Given a gematriya string, it will return the number it represents. -When passing a string, by default, it just adds up the numbers, regardless of place. By passing `true` as a second parameter, it will treat it as being ordered, as per the output (see below). +When passing a string, by default, it just adds up the numbers, regardless of place. By passing `{order: true}` as a second parameter, it will treat it as being ordered, as per the output (see below). -When passing a number, a second parameter is available, `limit`. This will limit the length of the returned string to a number of digits. For example: +When passing a number, an optional options object is available as a second parameter. Setting a number as a value for the limit key will limit the length of the returned string to a number of digits. Setting false as the value for the punctuate key will remove double and single quoatation marks in the returned string. Like this: ```js gematriya(5774) // התשע"ד - ordinary -gematriya(5774, 3) // תשע"ד - cropped to 774 -gematriya(5774, 7) // התשע"ד - kept at 5774 +gematriya(5774, {limit: 3}) // תשע"ד - cropped to 774 +gematriya(5774, {limit: 7}) // התשע"ד - kept at 5774 +gematriya(5774, {punctuate: false}) // 'התשעד' - removed quotation marks +gematriya(5774, {punctuate: true}) // 'התשע"ד' - with quotation marks +gematriya('התשעד', {order: true}) // 5774 - treats the characters as an ordered number +gematriya('התשעד', {order: false}) // 779 - Adds up all the characters ``` ## License diff --git a/gematriya.js b/gematriya.js index a61cba7..0d1ed8b 100644 --- a/gematriya.js +++ b/gematriya.js @@ -60,11 +60,26 @@ letters[numbers[i]] = i; } - function gematriya(num, limit) { + function gematriya(num, options) { + if (options === undefined) { + var options = {limit: false, punctuate: true, order: false } + } + if (typeof num !== 'number' && typeof num !== 'string') { throw new TypeError('non-number or string given to gematriya()'); } + + if (typeof options !== 'object' || options === null){ + throw new TypeError('An object was not given as second argument') + } + + var limit = options.limit + var punctuate = options.punctuate + var order = options.order + + var str = typeof num === 'string'; + if (str) { num = num.replace(/('|")/g,''); } @@ -75,7 +90,7 @@ num = num.map(function g(n,i){ if (str) { - return limit && numbers[n] < numbers[num[i - 1]] && numbers[n] < 100 ? numbers[n] * 1000 : numbers[n]; + return order && numbers[n] < numbers[num[i - 1]] && numbers[n] < 100 ? numbers[n] * 1000 : numbers[n]; } else { if (parseInt(n, 10) * Math.pow(10, i) > 1000) { return g(n, i-3); @@ -90,11 +105,13 @@ }, 0); } else { num = num.reverse().join('').replace(/יה/g,'טו').replace(/יו/g,'טז').split(''); - - if (num.length === 1) { - num.push("'"); - } else if (num.length > 1) { - num.splice(-1, 0, '"'); + + if (punctuate) { + if (num.length === 1) { + num.push("'"); + } else if (num.length > 1) { + num.splice(-1, 0, '"'); + } } return num.join('');