├── bower.json ├── package.json ├── LICENSE ├── README.md └── gematriya.js /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gematriya", 3 | "main": "index.js", 4 | "version": "1.0.1", 5 | "authors": [ 6 | "Scimonster " 7 | ], 8 | "description": "Convert numbers to gematriya representation, and vice-versa.", 9 | "main": "gematriya.js", 10 | "moduleType": [ 11 | "node" 12 | ], 13 | "keywords": [ 14 | "hebrew", 15 | "gematriya", 16 | "numbers" 17 | ], 18 | "license": "MIT", 19 | "homepage": "https://github.com/Scimonster/js-gematriya" 20 | } 21 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gematriya", 3 | "version": "2.0.0", 4 | "author": "Eyal Schachter (https://github.com/Scimonster)", 5 | "description": "Convert numbers to gematriya representation, and vice-versa.", 6 | "keywords": [ 7 | "hebrew", 8 | "gematriya", 9 | "numbers" 10 | ], 11 | "main": "gematriya.js", 12 | "homepage": "https://github.com/Scimonster/js-gematriya", 13 | "bugs": "https://github.com/Scimonster/js-gematriya/issues", 14 | "license": "MIT", 15 | "repository": { 16 | "type": "git", 17 | "url": "https://github.com/Scimonster/js-gematriya.git" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Eyal Schachter 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Gematriya 2 | 3 | [Gematriya/Gematria](https://en.wikipedia.org/wiki/Gematria), or perhaps more accurately, [Hebrew numerals](https://en.wikipedia.org/wiki/Hebrew_numerals), is a system of writing numbers as Hebrew letters. This JavaScript module allows for easy conversion between gematriya and JavaScript `Number` types. 4 | 5 | This code was originally written for [hebcal/hebcal-js](https://github.com/hebcal/hebcal-js). 6 | 7 | ## Install 8 | 9 | Install gematriya from NPM, bower, or just take the `gematriya.js` script from this repo. 10 | 11 | ```bash 12 | npm install gematriya 13 | bower install gematriya 14 | ``` 15 | 16 | ## API 17 | 18 | On the client side, the API is available through the global function `gematriya`. In Node, `require('gematriya')`. 19 | 20 | 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. 21 | 22 | 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). 23 | 24 | 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 quotation marks in the returned string. Setting `geresh` to false will use ASCII single/double quotes instead of Hebrew geresh/gershayim Unicode characters. Like this: 25 | 26 | ```js 27 | gematriya(5774) // התשע״ד - ordinary 28 | gematriya(5774, {limit: 3}) // תשע״ד - cropped to 774 29 | gematriya(5774, {limit: 7}) // התשע״ד - kept at 5774 30 | gematriya(5774, {punctuate: false}) // 'התשעד' - removed quotation marks 31 | gematriya(5774, {punctuate: true}) // 'התשע״ד' - with quotation marks 32 | gematriya(5774, {geresh: false}) // 'התשע"ד' - with quotation marks 33 | gematriya(5774, {punctuate: false, limit: 3}) // 'תשעד' - options can be combined 34 | gematriya(3) // 'ג׳' - note the geresh is RTL 35 | gematriya(3, {geresh: false}) // - "ג'" - the apostrophe is not 36 | gematriya('התשעד', {order: true}) // 5774 - treats the characters as an ordered number 37 | gematriya('התשעד', {order: false}) // 779 - Adds up all the characters 38 | ``` 39 | 40 | ## License 41 | 42 | Licensed MIT. 43 | -------------------------------------------------------------------------------- /gematriya.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Convert numbers to gematriya representation, and vice-versa. 3 | * 4 | * Licensed MIT. 5 | * 6 | * Copyright (c) 2014 Eyal Schachter 7 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | (function(){ 28 | var letters = {}, numbers = { 29 | '': 0, 30 | א: 1, 31 | ב: 2, 32 | ג: 3, 33 | ד: 4, 34 | ה: 5, 35 | ו: 6, 36 | ז: 7, 37 | ח: 8, 38 | ט: 9, 39 | י: 10, 40 | כ: 20, 41 | ל: 30, 42 | מ: 40, 43 | נ: 50, 44 | ס: 60, 45 | ע: 70, 46 | פ: 80, 47 | צ: 90, 48 | ק: 100, 49 | ר: 200, 50 | ש: 300, 51 | ת: 400, 52 | תק: 500, 53 | תר: 600, 54 | תש: 700, 55 | תת: 800, 56 | תתק: 900, 57 | תתר: 1000 58 | }, i; 59 | for (i in numbers) { 60 | letters[numbers[i]] = i; 61 | } 62 | 63 | function gematriya(num, options) { 64 | if (options === undefined) { 65 | var options = {limit: false, punctuate: true, order: false, geresh: true}; 66 | } 67 | 68 | if (typeof num !== 'number' && typeof num !== 'string') { 69 | throw new TypeError('non-number or string given to gematriya()'); 70 | } 71 | 72 | if (typeof options !== 'object' || options === null){ 73 | throw new TypeError('An object was not given as second argument') 74 | } 75 | 76 | var limit = options.limit; 77 | var order = options.order; 78 | var punctuate = typeof options.punctuate === 'undefined' ? true : options.punctuate; 79 | var geresh = typeof options.geresh === 'undefined' && punctuate ? true : options.geresh; 80 | 81 | var str = typeof num === 'string'; 82 | 83 | if (str) { 84 | num = num.replace(/('|")/g,''); 85 | } 86 | num = num.toString().split('').reverse(); 87 | if (!str && limit) { 88 | num = num.slice(0, limit); 89 | } 90 | 91 | num = num.map(function g(n,i){ 92 | if (str) { 93 | return order && numbers[n] < numbers[num[i - 1]] && numbers[n] < 100 ? numbers[n] * 1000 : numbers[n]; 94 | } else { 95 | if (parseInt(n, 10) * Math.pow(10, i) > 1000) { 96 | return g(n, i-3); 97 | } 98 | return letters[parseInt(n, 10) * Math.pow(10, i)]; 99 | } 100 | }); 101 | 102 | if (str) { 103 | return num.reduce(function(o,t){ 104 | return o + t; 105 | }, 0); 106 | } else { 107 | num = num.reverse().join('').replace(/יה/g,'טו').replace(/יו/g,'טז').split(''); 108 | 109 | if (punctuate || geresh) { 110 | if (num.length === 1) { 111 | num.push(geresh ? '׳' : "'"); 112 | } else if (num.length > 1) { 113 | num.splice(-1, 0, geresh ? '״' : '"'); 114 | } 115 | } 116 | 117 | return num.join(''); 118 | } 119 | } 120 | 121 | if (typeof module !== 'undefined') { 122 | module.exports = gematriya; 123 | } else { 124 | window.gematriya = gematriya; 125 | } 126 | })(); 127 | --------------------------------------------------------------------------------