@jaredwray/fumanchu 2.1.0 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -16
- package/dist/index.d.ts +64 -0
- package/dist/index.js +2135 -0
- package/package.json +87 -68
- package/site/logo.svg +19 -0
- package/helpers.js +0 -61
- package/index.d.ts +0 -4
- package/index.js +0 -8
- package/lib/array.js +0 -822
- package/lib/code.js +0 -80
- package/lib/collection.js +0 -67
- package/lib/comparison.js +0 -604
- package/lib/date.js +0 -24
- package/lib/fs.js +0 -75
- package/lib/html.js +0 -218
- package/lib/i18n.js +0 -48
- package/lib/index.js +0 -24
- package/lib/inflection.js +0 -78
- package/lib/logging.js +0 -8
- package/lib/markdown.js +0 -99
- package/lib/match.js +0 -61
- package/lib/math.js +0 -285
- package/lib/md.js +0 -119
- package/lib/misc.js +0 -77
- package/lib/number.js +0 -204
- package/lib/object.js +0 -290
- package/lib/path.js +0 -181
- package/lib/regex.js +0 -50
- package/lib/string.js +0 -769
- package/lib/url.js +0 -132
- package/lib/utils/falsey.js +0 -46
- package/lib/utils/html.js +0 -82
- package/lib/utils/index.js +0 -91
- package/lib/utils/utils.js +0 -42
package/lib/math.js
DELETED
|
@@ -1,285 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var isNumber = require('is-number');
|
|
4
|
-
var utils = require('./utils');
|
|
5
|
-
var helpers = module.exports;
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Return the magnitude of `a`.
|
|
9
|
-
*
|
|
10
|
-
* @param {Number} `a`
|
|
11
|
-
* @return {Number}
|
|
12
|
-
* @api public
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
helpers.abs = function(num) {
|
|
16
|
-
if (!isNumber(num)) {
|
|
17
|
-
throw new TypeError('expected a number');
|
|
18
|
-
}
|
|
19
|
-
return Math.abs(num);
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Return the sum of `a` plus `b`.
|
|
24
|
-
*
|
|
25
|
-
* @param {Number} `a`
|
|
26
|
-
* @param {Number} `b`
|
|
27
|
-
* @return {Number}
|
|
28
|
-
* @api public
|
|
29
|
-
*/
|
|
30
|
-
|
|
31
|
-
helpers.add = function(a, b) {
|
|
32
|
-
if (isNumber(a) && isNumber(b)) {
|
|
33
|
-
return Number(a) + Number(b);
|
|
34
|
-
}
|
|
35
|
-
return '';
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Returns the average of all numbers in the given array.
|
|
40
|
-
*
|
|
41
|
-
* ```handlebars
|
|
42
|
-
* {{avg "[1, 2, 3, 4, 5]"}}
|
|
43
|
-
* <!-- results in: '3' -->
|
|
44
|
-
* ```
|
|
45
|
-
*
|
|
46
|
-
* @param {Array} `array` Array of numbers to add up.
|
|
47
|
-
* @return {Number}
|
|
48
|
-
* @api public
|
|
49
|
-
*/
|
|
50
|
-
|
|
51
|
-
helpers.avg = function() {
|
|
52
|
-
var args = [].concat.apply([], arguments);
|
|
53
|
-
// remove handlebars options object
|
|
54
|
-
args.pop();
|
|
55
|
-
return helpers.sum(args) / args.length;
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Get the `Math.ceil()` of the given value.
|
|
60
|
-
*
|
|
61
|
-
* @param {Number} `value`
|
|
62
|
-
* @return {Number}
|
|
63
|
-
* @api public
|
|
64
|
-
*/
|
|
65
|
-
|
|
66
|
-
helpers.ceil = function(num) {
|
|
67
|
-
if (!isNumber(num)) {
|
|
68
|
-
throw new TypeError('expected a number');
|
|
69
|
-
}
|
|
70
|
-
return Math.ceil(num);
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Divide `a` by `b`
|
|
75
|
-
*
|
|
76
|
-
* @param {Number} `a` numerator
|
|
77
|
-
* @param {Number} `b` denominator
|
|
78
|
-
* @api public
|
|
79
|
-
*/
|
|
80
|
-
|
|
81
|
-
helpers.divide = function(a, b) {
|
|
82
|
-
if (!isNumber(a)) {
|
|
83
|
-
throw new TypeError('expected the first argument to be a number');
|
|
84
|
-
}
|
|
85
|
-
if (!isNumber(b)) {
|
|
86
|
-
throw new TypeError('expected the second argument to be a number');
|
|
87
|
-
}
|
|
88
|
-
return Number(a) / Number(b);
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Get the `Math.floor()` of the given value.
|
|
93
|
-
*
|
|
94
|
-
* @param {Number} `value`
|
|
95
|
-
* @return {Number}
|
|
96
|
-
* @api public
|
|
97
|
-
*/
|
|
98
|
-
|
|
99
|
-
helpers.floor = function(num) {
|
|
100
|
-
if (!isNumber(num)) {
|
|
101
|
-
throw new TypeError('expected a number');
|
|
102
|
-
}
|
|
103
|
-
return Math.floor(num);
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Return the difference of `a` minus `b`.
|
|
108
|
-
*
|
|
109
|
-
* @param {Number} `a`
|
|
110
|
-
* @param {Number} `b`
|
|
111
|
-
* @alias subtract
|
|
112
|
-
* @api public
|
|
113
|
-
*/
|
|
114
|
-
|
|
115
|
-
helpers.minus = function(a, b) {
|
|
116
|
-
if (!isNumber(a)) {
|
|
117
|
-
throw new TypeError('expected the first argument to be a number');
|
|
118
|
-
}
|
|
119
|
-
if (!isNumber(b)) {
|
|
120
|
-
throw new TypeError('expected the second argument to be a number');
|
|
121
|
-
}
|
|
122
|
-
return Number(a) - Number(b);
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Get the remainder of a division operation.
|
|
127
|
-
*
|
|
128
|
-
* @param {Number} `a`
|
|
129
|
-
* @param {Number} `b`
|
|
130
|
-
* @return {Number}
|
|
131
|
-
* @api public
|
|
132
|
-
*/
|
|
133
|
-
|
|
134
|
-
helpers.modulo = function(a, b) {
|
|
135
|
-
if (!isNumber(a)) {
|
|
136
|
-
throw new TypeError('expected the first argument to be a number');
|
|
137
|
-
}
|
|
138
|
-
if (!isNumber(b)) {
|
|
139
|
-
throw new TypeError('expected the second argument to be a number');
|
|
140
|
-
}
|
|
141
|
-
return Number(a) % Number(b);
|
|
142
|
-
};
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Return the product of `a` times `b`.
|
|
146
|
-
*
|
|
147
|
-
* @param {Number} `a` factor
|
|
148
|
-
* @param {Number} `b` multiplier
|
|
149
|
-
* @return {Number}
|
|
150
|
-
* @alias times
|
|
151
|
-
* @api public
|
|
152
|
-
*/
|
|
153
|
-
|
|
154
|
-
helpers.multiply = function(a, b) {
|
|
155
|
-
if (!isNumber(a)) {
|
|
156
|
-
throw new TypeError('expected the first argument to be a number');
|
|
157
|
-
}
|
|
158
|
-
if (!isNumber(b)) {
|
|
159
|
-
throw new TypeError('expected the second argument to be a number');
|
|
160
|
-
}
|
|
161
|
-
return Number(a) * Number(b);
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Add `a` by `b`.
|
|
166
|
-
*
|
|
167
|
-
* @param {Number} `a` factor
|
|
168
|
-
* @param {Number} `b` multiplier
|
|
169
|
-
* @api public
|
|
170
|
-
*/
|
|
171
|
-
|
|
172
|
-
helpers.plus = function(a, b) {
|
|
173
|
-
if (!isNumber(a)) {
|
|
174
|
-
throw new TypeError('expected the first argument to be a number');
|
|
175
|
-
}
|
|
176
|
-
if (!isNumber(b)) {
|
|
177
|
-
throw new TypeError('expected the second argument to be a number');
|
|
178
|
-
}
|
|
179
|
-
return Number(a) + Number(b);
|
|
180
|
-
};
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* Generate a random number between two values
|
|
184
|
-
*
|
|
185
|
-
* @param {Number} `min`
|
|
186
|
-
* @param {Number} `max`
|
|
187
|
-
* @return {String}
|
|
188
|
-
* @api public
|
|
189
|
-
*/
|
|
190
|
-
|
|
191
|
-
helpers.random = function(min, max) {
|
|
192
|
-
if (!isNumber(min)) {
|
|
193
|
-
throw new TypeError('expected minimum to be a number');
|
|
194
|
-
}
|
|
195
|
-
if (!isNumber(max)) {
|
|
196
|
-
throw new TypeError('expected maximum to be a number');
|
|
197
|
-
}
|
|
198
|
-
return utils.random(min, max);
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* Get the remainder when `a` is divided by `b`.
|
|
203
|
-
*
|
|
204
|
-
* @param {Number} `a` a
|
|
205
|
-
* @param {Number} `b` b
|
|
206
|
-
* @api public
|
|
207
|
-
*/
|
|
208
|
-
|
|
209
|
-
helpers.remainder = function(a, b) {
|
|
210
|
-
return a % b;
|
|
211
|
-
};
|
|
212
|
-
|
|
213
|
-
/**
|
|
214
|
-
* Round the given number.
|
|
215
|
-
*
|
|
216
|
-
* @param {Number} `number`
|
|
217
|
-
* @return {Number}
|
|
218
|
-
* @api public
|
|
219
|
-
*/
|
|
220
|
-
|
|
221
|
-
helpers.round = function(num) {
|
|
222
|
-
if (!isNumber(num)) {
|
|
223
|
-
throw new TypeError('expected a number');
|
|
224
|
-
}
|
|
225
|
-
return Math.round(num);
|
|
226
|
-
};
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* Return the product of `a` minus `b`.
|
|
230
|
-
*
|
|
231
|
-
* @param {Number} `a`
|
|
232
|
-
* @param {Number} `b`
|
|
233
|
-
* @return {Number}
|
|
234
|
-
* @alias minus
|
|
235
|
-
* @api public
|
|
236
|
-
*/
|
|
237
|
-
|
|
238
|
-
helpers.subtract = function(a, b) {
|
|
239
|
-
if (!isNumber(a)) {
|
|
240
|
-
throw new TypeError('expected the first argument to be a number');
|
|
241
|
-
}
|
|
242
|
-
if (!isNumber(b)) {
|
|
243
|
-
throw new TypeError('expected the second argument to be a number');
|
|
244
|
-
}
|
|
245
|
-
return Number(a) - Number(b);
|
|
246
|
-
};
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* Returns the sum of all numbers in the given array.
|
|
250
|
-
*
|
|
251
|
-
* ```handlebars
|
|
252
|
-
* {{sum "[1, 2, 3, 4, 5]"}}
|
|
253
|
-
* <!-- results in: '15' -->
|
|
254
|
-
* ```
|
|
255
|
-
* @param {Array} `array` Array of numbers to add up.
|
|
256
|
-
* @return {Number}
|
|
257
|
-
* @api public
|
|
258
|
-
*/
|
|
259
|
-
|
|
260
|
-
helpers.sum = function() {
|
|
261
|
-
var args = [].concat.apply([], arguments);
|
|
262
|
-
var len = args.length;
|
|
263
|
-
var sum = 0;
|
|
264
|
-
|
|
265
|
-
while (len--) {
|
|
266
|
-
if (utils.isNumber(args[len])) {
|
|
267
|
-
sum += Number(args[len]);
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
return sum;
|
|
271
|
-
};
|
|
272
|
-
|
|
273
|
-
/**
|
|
274
|
-
* Multiply number `a` by number `b`.
|
|
275
|
-
*
|
|
276
|
-
* @param {Number} `a` factor
|
|
277
|
-
* @param {Number} `b` multiplier
|
|
278
|
-
* @return {Number}
|
|
279
|
-
* @alias multiply
|
|
280
|
-
* @api public
|
|
281
|
-
*/
|
|
282
|
-
|
|
283
|
-
helpers.times = function() {
|
|
284
|
-
return helpers.multiply.apply(this, arguments);
|
|
285
|
-
};
|
package/lib/md.js
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* helper-markdown <https://github.com/jonschlinkert/helper-markdown>
|
|
3
|
-
*
|
|
4
|
-
* Copyright (c) 2014 Jon Schlinkert, contributors.
|
|
5
|
-
* Licensed under the MIT license.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
'use strict';
|
|
9
|
-
|
|
10
|
-
var fs = require('fs');
|
|
11
|
-
var path = require('path');
|
|
12
|
-
const { Remarkable } = require('remarkable');
|
|
13
|
-
var extend = require('extend-shallow');
|
|
14
|
-
var exists = require('fs-exists-sync');
|
|
15
|
-
var ent = require('ent');
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Expose `md` helper
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
var helper = module.exports = function(name, options, cb) {
|
|
22
|
-
if (typeof options === 'function') {
|
|
23
|
-
cb = options;
|
|
24
|
-
options = {};
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (typeof cb !== 'function') {
|
|
28
|
-
return helper.sync.apply(this, arguments);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/* c8 ignore next 3 */
|
|
32
|
-
if (typeof this === 'undefined' || typeof this.app === 'undefined') {
|
|
33
|
-
throw new Error('md async helper expects `app` to be exposed on the context');
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
var opts = extend({cwd: process.cwd()}, this.options, options);
|
|
37
|
-
opts = extend({}, opts, opts.hash);
|
|
38
|
-
var md = markdown(opts);
|
|
39
|
-
|
|
40
|
-
var filepath = path.resolve(opts.cwd, name);
|
|
41
|
-
var view;
|
|
42
|
-
var str = '';
|
|
43
|
-
|
|
44
|
-
if (exists(filepath)) {
|
|
45
|
-
// create a collection to ensure middleware is consistent
|
|
46
|
-
this.app.create('mdfiles');
|
|
47
|
-
str = fs.readFileSync(filepath, 'utf8');
|
|
48
|
-
view = this.app.mdfile(filepath, {path: filepath, content: str});
|
|
49
|
-
} else {
|
|
50
|
-
view = this.app.find(name);
|
|
51
|
-
}
|
|
52
|
-
/* c8 ignore next 4 */
|
|
53
|
-
if (typeof view === 'undefined') {
|
|
54
|
-
cb(null, '');
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
view.content = ent.decode(md.render(view.content));
|
|
59
|
-
this.app.render(view, this.context, function(err, res) {
|
|
60
|
-
if (err) return cb(err);
|
|
61
|
-
cb(null, res.content);
|
|
62
|
-
});
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
helper.sync = function(name, options) {
|
|
66
|
-
var ctx = this || {};
|
|
67
|
-
var app = ctx.app || {};
|
|
68
|
-
|
|
69
|
-
var opts = extend({cwd: process.cwd()}, ctx.options, options);
|
|
70
|
-
opts = extend({}, opts, opts.hash);
|
|
71
|
-
var md = markdown(opts);
|
|
72
|
-
|
|
73
|
-
var filepath = path.resolve(opts.cwd, name);
|
|
74
|
-
var view;
|
|
75
|
-
var html = '';
|
|
76
|
-
var str = '';
|
|
77
|
-
|
|
78
|
-
if (exists(filepath)) {
|
|
79
|
-
str = fs.readFileSync(filepath, 'utf8');
|
|
80
|
-
html = ent.decode(md.render(str));
|
|
81
|
-
} else if (app.views) {
|
|
82
|
-
view = app.find(name);
|
|
83
|
-
if (view) {
|
|
84
|
-
html = view.content = ent.decode(md.render(view.content));
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
if (view && typeof view.compile === 'function') {
|
|
89
|
-
view.compile(opts);
|
|
90
|
-
var data = ctx.cache ? ctx.cache.data : {};
|
|
91
|
-
ctx = extend({}, data, view.data);
|
|
92
|
-
return view.fn(ctx);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (typeof this.compile === 'function') {
|
|
96
|
-
var fn = this.compile(html);
|
|
97
|
-
return fn(this);
|
|
98
|
-
}
|
|
99
|
-
return html;
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Shared settings for remarkable
|
|
104
|
-
*
|
|
105
|
-
* @param {Object} `options`
|
|
106
|
-
* @return {Object}
|
|
107
|
-
* @api private
|
|
108
|
-
*/
|
|
109
|
-
|
|
110
|
-
function markdown(options) {
|
|
111
|
-
let optsConfig = options || {};
|
|
112
|
-
optsConfig.breaks = true;
|
|
113
|
-
optsConfig.html = true;
|
|
114
|
-
optsConfig.langPrefix = 'lang-';
|
|
115
|
-
optsConfig.typographer = false;
|
|
116
|
-
optsConfig.xhtmlOut = false;
|
|
117
|
-
const md = new Remarkable(optsConfig);
|
|
118
|
-
return md;
|
|
119
|
-
}
|
package/lib/misc.js
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var util = require('handlebars-utils');
|
|
4
|
-
var utils = require('./utils');
|
|
5
|
-
var helpers = module.exports;
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Block helper for exposing private `@` variables on the context
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
helpers.frame = require('handlebars-helper-create-frame');
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Return the given value of `prop` from `this.options`.
|
|
15
|
-
*
|
|
16
|
-
* ```handlebars
|
|
17
|
-
* <!-- context = {options: {a: {b: {c: 'ddd'}}}} -->
|
|
18
|
-
* {{option "a.b.c"}}
|
|
19
|
-
* <!-- results => `ddd` -->
|
|
20
|
-
* ```
|
|
21
|
-
* @param {String} `prop`
|
|
22
|
-
* @return {any}
|
|
23
|
-
* @api public
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
helpers.option = function(prop, locals, options) {
|
|
27
|
-
return utils.get(util.options(this, locals, options), prop);
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Block helper that renders the block without taking any arguments.
|
|
32
|
-
*
|
|
33
|
-
* @param {Object} `options`
|
|
34
|
-
* @return {String}
|
|
35
|
-
* @block
|
|
36
|
-
* @api public
|
|
37
|
-
*/
|
|
38
|
-
|
|
39
|
-
helpers.noop = function(options) {
|
|
40
|
-
return options.fn(this);
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Get the native type of the given `value`
|
|
45
|
-
*
|
|
46
|
-
* ```handlebars
|
|
47
|
-
* {{typeOf 1}}
|
|
48
|
-
* //=> 'number'
|
|
49
|
-
* {{typeOf "1"}}
|
|
50
|
-
* //=> 'string'
|
|
51
|
-
* {{typeOf "foo"}}
|
|
52
|
-
* //=> 'string'
|
|
53
|
-
* ```
|
|
54
|
-
* @param {any} `value`
|
|
55
|
-
* @return {String} Returns the type of value.
|
|
56
|
-
* @api public
|
|
57
|
-
*/
|
|
58
|
-
|
|
59
|
-
helpers.typeOf = require('kind-of');
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Block helper that builds the context for the block
|
|
63
|
-
* from the options hash.
|
|
64
|
-
*
|
|
65
|
-
* @param {Object} `options` Handlebars provided options object.
|
|
66
|
-
* @contributor Vladimir Kuznetsov <https://github.com/mistakster>
|
|
67
|
-
* @block
|
|
68
|
-
* @api public
|
|
69
|
-
*/
|
|
70
|
-
|
|
71
|
-
helpers.withHash = function(options) {
|
|
72
|
-
if (options.hash && Object.keys(options.hash).length) {
|
|
73
|
-
return options.fn(options.hash);
|
|
74
|
-
} else {
|
|
75
|
-
return options.inverse(this);
|
|
76
|
-
}
|
|
77
|
-
};
|
package/lib/number.js
DELETED
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var isNumber = require('is-number');
|
|
4
|
-
var util = require('handlebars-utils');
|
|
5
|
-
var utils = require('./utils');
|
|
6
|
-
var helpers = module.exports;
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Format a number to it's equivalent in bytes. If a string is passed,
|
|
10
|
-
* it's length will be formatted and returned.
|
|
11
|
-
*
|
|
12
|
-
* **Examples:**
|
|
13
|
-
*
|
|
14
|
-
* - `'foo' => 3 B`
|
|
15
|
-
* - `13661855 => 13.66 MB`
|
|
16
|
-
* - `825399 => 825.39 kB`
|
|
17
|
-
* - `1396 => 1.4 kB`
|
|
18
|
-
*
|
|
19
|
-
* @param {Number|String} `number`
|
|
20
|
-
* @return {String}
|
|
21
|
-
* @api public
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
helpers.bytes = function(number, precision, options) {
|
|
25
|
-
if (number == null) return '0 B';
|
|
26
|
-
|
|
27
|
-
if (!utils.isNumber(number)) {
|
|
28
|
-
number = number.length;
|
|
29
|
-
if (!number) return '0 B';
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (!utils.isNumber(precision)) {
|
|
33
|
-
precision = 2;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
var abbr = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
|
37
|
-
precision = Math.pow(10, precision);
|
|
38
|
-
number = Number(number);
|
|
39
|
-
|
|
40
|
-
var len = abbr.length - 1;
|
|
41
|
-
while (len-- >= 0) {
|
|
42
|
-
var size = Math.pow(10, len * 3);
|
|
43
|
-
if (size <= (number + 1)) {
|
|
44
|
-
number = Math.round(number * precision / size) / precision;
|
|
45
|
-
number += ' ' + abbr[len];
|
|
46
|
-
break;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return number;
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Add commas to numbers
|
|
55
|
-
*
|
|
56
|
-
* @param {Number} `num`
|
|
57
|
-
* @return {Number}
|
|
58
|
-
* @api public
|
|
59
|
-
*/
|
|
60
|
-
|
|
61
|
-
helpers.addCommas = function(num) {
|
|
62
|
-
return num.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1,');
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Convert a string or number to a formatted phone number.
|
|
67
|
-
*
|
|
68
|
-
* @param {Number|String} `num` The phone number to format, e.g. `8005551212`
|
|
69
|
-
* @return {Number} Formatted phone number: `(800) 555-1212`
|
|
70
|
-
* @source http://bit.ly/QlPmPr
|
|
71
|
-
* @api public
|
|
72
|
-
*/
|
|
73
|
-
|
|
74
|
-
helpers.phoneNumber = function(num) {
|
|
75
|
-
num = num.toString();
|
|
76
|
-
|
|
77
|
-
return '(' + num.substr(0, 3) + ') '
|
|
78
|
-
+ num.substr(3, 3) + '-'
|
|
79
|
-
+ num.substr(6, 4);
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Abbreviate numbers to the given number of `precision`. This is for
|
|
84
|
-
* general numbers, not size in bytes.
|
|
85
|
-
*
|
|
86
|
-
* @param {Number} `number`
|
|
87
|
-
* @param {Number} `precision`
|
|
88
|
-
* @return {String}
|
|
89
|
-
* @api public
|
|
90
|
-
*/
|
|
91
|
-
|
|
92
|
-
helpers.toAbbr = function(number, precision) {
|
|
93
|
-
if (!utils.isNumber(number)) {
|
|
94
|
-
number = 0;
|
|
95
|
-
}
|
|
96
|
-
if (util.isUndefined(precision)) {
|
|
97
|
-
precision = 2;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
number = Number(number);
|
|
101
|
-
// 2 decimal places => 100, 3 => 1000, etc.
|
|
102
|
-
precision = Math.pow(10, precision);
|
|
103
|
-
var abbr = ['k', 'm', 'b', 't', 'q'];
|
|
104
|
-
var len = abbr.length - 1;
|
|
105
|
-
|
|
106
|
-
while (len >= 0) {
|
|
107
|
-
var size = Math.pow(10, (len + 1) * 3);
|
|
108
|
-
if (size <= (number + 1)) {
|
|
109
|
-
number = Math.round(number * precision / size) / precision;
|
|
110
|
-
number += abbr[len];
|
|
111
|
-
break;
|
|
112
|
-
}
|
|
113
|
-
len--;
|
|
114
|
-
}
|
|
115
|
-
return number;
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Returns a string representing the given number in exponential notation.
|
|
120
|
-
*
|
|
121
|
-
* ```handlebars
|
|
122
|
-
* {{toExponential number digits}};
|
|
123
|
-
* ```
|
|
124
|
-
* @param {Number} `number`
|
|
125
|
-
* @param {Number} `fractionDigits` Optional. An integer specifying the number of digits to use after the decimal point. Defaults to as many digits as necessary to specify the number.
|
|
126
|
-
* @return {Number}
|
|
127
|
-
* @api public
|
|
128
|
-
*/
|
|
129
|
-
|
|
130
|
-
helpers.toExponential = function(number, digits) {
|
|
131
|
-
if (!utils.isNumber(number)) {
|
|
132
|
-
number = 0;
|
|
133
|
-
}
|
|
134
|
-
if (util.isUndefined(digits)) {
|
|
135
|
-
digits = 0;
|
|
136
|
-
}
|
|
137
|
-
return Number(number).toExponential(digits);
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* Formats the given number using fixed-point notation.
|
|
142
|
-
*
|
|
143
|
-
* ```handlebars
|
|
144
|
-
* {{toFixed "1.1234" 2}}
|
|
145
|
-
* //=> '1.12'
|
|
146
|
-
* ```
|
|
147
|
-
* @param {Number} `number`
|
|
148
|
-
* @param {Number} `digits` (Optional) The number of digits to appear after the decimal point; this may be a value between 0 and 20. If this argument is omitted, it is treated as 0.
|
|
149
|
-
* @return {String} A string representing the given number using fixed-point notation.
|
|
150
|
-
* @api public
|
|
151
|
-
*/
|
|
152
|
-
|
|
153
|
-
helpers.toFixed = function(number, digits) {
|
|
154
|
-
if (!utils.isNumber(number)) {
|
|
155
|
-
number = 0;
|
|
156
|
-
}
|
|
157
|
-
if (!isNumber(digits)) {
|
|
158
|
-
digits = 0;
|
|
159
|
-
}
|
|
160
|
-
return Number(number).toFixed(digits);
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
/**
|
|
164
|
-
* @param {Number} `number`
|
|
165
|
-
* @return {Number}
|
|
166
|
-
* @api public
|
|
167
|
-
*/
|
|
168
|
-
|
|
169
|
-
helpers.toFloat = function(number) {
|
|
170
|
-
return parseFloat(number);
|
|
171
|
-
};
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* @param {Number} `number`
|
|
175
|
-
* @return {Number}
|
|
176
|
-
* @api public
|
|
177
|
-
*/
|
|
178
|
-
|
|
179
|
-
helpers.toInt = function(number) {
|
|
180
|
-
return parseInt(number, 10);
|
|
181
|
-
};
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* Returns a string representing the `Number` object to the specified precision.
|
|
185
|
-
*
|
|
186
|
-
* ```handlebars
|
|
187
|
-
* {{toPrecision "1.1234" 2}}
|
|
188
|
-
* //=> '1.1'
|
|
189
|
-
* ```
|
|
190
|
-
* @param {Number} `number`
|
|
191
|
-
* @param {Number} `precision` (Optional) An integer specifying the number of significant digits. If precison is not between 1 and 100 (inclusive), it will be coerced to `0`.
|
|
192
|
-
* @return {String} A string representing a Number object in fixed-point or exponential notation rounded to precision significant digits.
|
|
193
|
-
* @api public
|
|
194
|
-
*/
|
|
195
|
-
|
|
196
|
-
helpers.toPrecision = function(number, precision) {
|
|
197
|
-
if (!utils.isNumber(number)) {
|
|
198
|
-
number = 0;
|
|
199
|
-
}
|
|
200
|
-
if (!isNumber(precision)) {
|
|
201
|
-
precision = 1;
|
|
202
|
-
}
|
|
203
|
-
return Number(number).toPrecision(precision);
|
|
204
|
-
};
|