-
Notifications
You must be signed in to change notification settings - Fork 5
/
gematriya.js
126 lines (113 loc) · 3.21 KB
/
gematriya.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Convert numbers to gematriya representation, and vice-versa.
*
* Licensed MIT.
*
* Copyright (c) 2014 Eyal Schachter
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
(function(){
var letters = {}, numbers = {
'': 0,
א: 1,
ב: 2,
ג: 3,
ד: 4,
ה: 5,
ו: 6,
ז: 7,
ח: 8,
ט: 9,
י: 10,
כ: 20,
ל: 30,
מ: 40,
נ: 50,
ס: 60,
ע: 70,
פ: 80,
צ: 90,
ק: 100,
ר: 200,
ש: 300,
ת: 400,
תק: 500,
תר: 600,
תש: 700,
תת: 800,
תתק: 900,
תתר: 1000
}, i;
for (i in numbers) {
letters[numbers[i]] = i;
}
function gematriya(num, options) {
if (options === undefined) {
var options = {limit: false, punctuate: true, order: false, geresh: true};
}
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 order = options.order;
var punctuate = typeof options.punctuate === 'undefined' ? true : options.punctuate;
var geresh = typeof options.geresh === 'undefined' && punctuate ? true : options.geresh;
var str = typeof num === 'string';
if (str) {
num = num.replace(/('|")/g,'');
}
num = num.toString().split('').reverse();
if (!str && limit) {
num = num.slice(0, limit);
}
num = num.map(function g(n,i){
if (str) {
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);
}
return letters[parseInt(n, 10) * Math.pow(10, i)];
}
});
if (str) {
return num.reduce(function(o,t){
return o + t;
}, 0);
} else {
num = num.reverse().join('').replace(/יה/g,'טו').replace(/יו/g,'טז').split('');
if (punctuate || geresh) {
if (num.length === 1) {
num.push(geresh ? '׳' : "'");
} else if (num.length > 1) {
num.splice(-1, 0, geresh ? '״' : '"');
}
}
return num.join('');
}
}
if (typeof module !== 'undefined') {
module.exports = gematriya;
} else {
window.gematriya = gematriya;
}
})();