Skip to content

Commit

Permalink
Merge pull request #1 from peacestone/master
Browse files Browse the repository at this point in the history
Add option to remove quotations marks from returned strings
  • Loading branch information
Scimonster authored Feb 14, 2019
2 parents bce3cb1 + e65d1b8 commit f9d5e9f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand All @@ -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
Expand Down
31 changes: 24 additions & 7 deletions gematriya.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,'');
}
Expand All @@ -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);
Expand All @@ -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('');
Expand Down

0 comments on commit f9d5e9f

Please sign in to comment.