-
Notifications
You must be signed in to change notification settings - Fork 0
/
extend-native.js
232 lines (182 loc) · 7.64 KB
/
extend-native.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Add methods to native objects
+function() {
var periodUnitNL = {
jaar: 'y', jaren: 'y', maand: 'M', maanden: 'M', week: 'w', weken: 'w', dag: 'd', dagen: 'd',
uur: 'h', uren: 'h', minuut: 'm', minuten: 'm', seconde: 's', seconden: 's'
};
//Extend String
Object.defineProperties(String.prototype, {
capitalize: method(function() {
return this.charAt(0).toUpperCase() + this.slice(1);
}),
camelCase: method(function() {
return this.replace(/(^|\s)([a-z])/g , function(m, p1, p2) { return p1 + p2.toUpperCase(); });
}),
toInt: method(function() {
return this == "" ? null : parseInt(this);
}),
toDate: method(function() {
if (this == "") return null;
var date = moment(this + '');
if (!date.isValid()) date = moment(this + '', 'L');
date.defaultFormat = "L";
return date;
}),
toDateTime: method(function() {
if (this == "") return null;
var date = moment(this + '');
if (!date.isValid()) date = moment(this + '', 'L LT');
date.defaultFormat = "L LT";
return date.toString();
}),
toTime: method(function() {
if (this == "") return null;
var date = moment(this + '');
if (!date.isValid()) date = moment(this + '', 'LT');
date.defaultFormat = "LT";
return date.toString();
}),
dateFormat: method(function(formatAs) {
if (this == "") return null;
var date = moment(this + '');
if (!date.isValid()) date = moment(this + '', 'L');
var locale = getDocumentLocale('momentjs');
if (locale) date.locale(locale);
return formatAs ? date.format(formatAs) : date.format('L');
}),
addPeriod: method(function(duration, unit) {
if (typeof duration.amount !== 'undefined' && typeof duration.unit !== 'undefined') {
unit = duration.unit;
duration = duration.amount;
}
if (typeof periodUnitNL[unit] !== 'undefined') unit = periodUnitNL[unit];
var date = this.toDate();
return date ? date.add(unit, duration).toString() : date;
}),
substractPeriod: method(function(duration, unit) {
if (typeof duration.amount !== 'undefined' && typeof duration.unit !== 'undefined') {
unit = duration.unit;
duration = duration.amount;
}
if (typeof periodUnitNL[unit] !== 'undefined') unit = periodUnitNL[unit];
var date = this.toDate();
return date ? date.subtract(unit, duration).toString() : date;
}),
asList: method(function(listType) {
var lines = this.split("\n");
return lines.asList(listType);
}),
spelledOut: method(spelledOut),
toFloat: method(toFloat),
toNumber: method(toNumber),
toCurrency: method(toCurrency),
numberFormat: method(toNumberEn), //BC. Same as toNumber, but always use 'en' locale
localeNumberFormat: method(toCurrency) //BC, replaced with toCurrency
});
//Extend Number
Object.defineProperties(Number.prototype, {
spelledOut: method(spelledOut),
toFloat: method(toFloat),
toNumber: method(toNumber),
toCurrency: method(toCurrency),
numberFormat: method(toNumberEn), //Same as toNumber, but always use 'en' locale
localeNumberFormat: method(toCurrency) //BC, replaced with toCurrency
});
//Extend Array
Object.defineProperties(Array.prototype, {
asList: method(function(listType) {
if (!listType) listType = 'ul';
if (['ol','ul'].indexOf(listType) === -1) {
throw 'When representing as list, list type should be one of "ul" or "ol"';
}
var list = document.createElement(listType);
for (var i = 0; i < this.length; i++) {
if (this[i] === null || !this[i].toString().trim()) continue;
var item = document.createElement('li');
item.appendChild(document.createTextNode(this[i]));
list.appendChild(item);
};
return list.children.length ? list.outerHTML: null;
}),
sumFields: method(function(fieldName){
return this.reduce(function(acc, val) {
if (val[fieldName]) {
return acc + parseInt(val[fieldName]);
} else {
return acc;
}
}, 0);
}),
toString: method(function() {
if (this.length && (this[0] === '' || this[0] === null)) this.shift();
if (this.length === 0) return '';
if (this.length === 1) return this[0] + '';
return this.slice(0, -1).join(', ') + ' & ' + this[this.length -1];
})
});
// minimal polyfill
if (!Array.prototype.includes) {
Object.defineProperty(Array.prototype, 'includes', method(function(item) {
return this.indexOf(item) >= 0;
}));
}
// Polyfill
if (!Array.isArray) {
Object.defineProperty(Array, 'isArray', method(function(arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
}));
}
var fractionRegexp = /([,.])00$/;
var euroRegexp = /(€)\s+/;
//Format numbers as currency
function toCurrency(currency, useLocale) {
var locale = useLocale ? useLocale : getDocumentLocale('short');
var number = parseNumber(this);
if (!locale) return number;
if (!currency) currency = 'EUR';
var formater = new Intl.NumberFormat(locale, {style: 'currency', currency: currency});
number = formater.format(number);
number = number.replace(fractionRegexp, '$1-').replace(euroRegexp, '$1');
return number;
}
//Cast number or string to float
function toFloat() {
return this == "" ? null : parseNumber(this);
}
//Format numbers
function toNumber(precision, useLocale) {
if (typeof precision === 'undefined') precision = 0;
if (typeof useLocale === 'undefined') useLocale = getDocumentLocale('short');
var number = parseNumber(this);
var options = {maximumFractionDigits: precision};
var formater = new Intl.NumberFormat(useLocale, options);
number = formater.format(number);
return number;
}
//Format numbers in 'en' locale (BC)
function toNumberEn(precision) {
return toNumber.call(this, precision, 'en');
}
//Function for spelling numbers. For extending Number and String
function spelledOut() {
var number = parseInt(this);
if (!(this instanceof Number) && (!this.length || number.toString().length !== this.length)) return this.toString();
var locale = getDocumentLocale('short');
if (!locale) return this.toString();
if (['en','id','da','fr','in','nl'].indexOf(locale) === -1) return this.toString();
return spellit(locale)(number);
}
//Return object representing property descriptors with given getter method
function method(getter) {
if (!getter instanceof Function) {
throw 'Getter should be a function';
}
return {
enumerable: false,
configurable: false,
get: function() {
return getter;
}
};
}
}();