Skip to content

Commit e872c5b

Browse files
committed
added: Number.prototype.add()
1 parent c2137f0 commit e872c5b

3 files changed

Lines changed: 72 additions & 1 deletion

File tree

changes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ BETA ======= 1.8.1
2121
- added: `F.responseBinary(req, res, contentType, buffer, [type], [download], [headers])`
2222
- added: `SchemaBuilderEntity.filter(custom, [model], [reverse])`
2323
- added: `SchemaBuilderEntity.trim = true`(enable/disable trim strings (default: true))
24+
- added: `Number.prototype.add(value, [decimals])` -> supports percentage
2425
- added: `Date.prototype.toUTC([ticksOnly])`
2526
- added: `Date.prototype.extend()` -> extend current datetime about new date or time (more in documentation)
2627
- added: `F.stats.request.mobile`, `F.stats.request.desktop`

test/test-utils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ function prototypeNumber() {
4545
assert.ok(number.floor(4) === 10.1034, 'floor number: 4 decimals');
4646
assert.ok(number.floor(0) === 10, 'floor number: 0 decimals');
4747
assert.ok(number.hex() === 'A.1A7AB75643028', 'number to hex');
48+
assert.ok(number.add('10%', 0) === 1, 'add number: 1');
49+
assert.ok(number.add('+10%', 0) === 11, 'add number: 2');
50+
assert.ok(number.add('-10%', 0) === 9, 'add number: 3');
51+
assert.ok(number.add('*2', 0) === 20, 'add number: 4');
4852
}
4953

5054
// test: string prototype

utils.js

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3130,6 +3130,72 @@ Number.prototype.format = function(decimals, separator, separatorDecimal) {
31303130
return minus + output + (dec.length > 0 ? separatorDecimal + dec : '');
31313131
};
31323132

3133+
Number.prototype.add = function(value, decimals) {
3134+
3135+
if (value === undefined || value === null)
3136+
return this;
3137+
3138+
if (typeof(value) === NUMBER)
3139+
return this + value;
3140+
3141+
var first = value.charCodeAt(0);
3142+
var is = false;
3143+
3144+
if (first < 48 || first > 57) {
3145+
is = true;
3146+
value = value.substring(1);
3147+
}
3148+
3149+
var length = value.length;
3150+
var isPercentage = false;
3151+
var num;
3152+
3153+
if (value[length - 1] === '%') {
3154+
value = value.substring(0, length - 1);
3155+
isPercentage = true;
3156+
3157+
if (is) {
3158+
var tmp = ((value.parseFloat() / 100) + 1);
3159+
if (first === 43 || first === 42)
3160+
num = this * tmp;
3161+
else
3162+
num = this / tmp;
3163+
return decimals !== undefined ? num.floor(decimals) : num;
3164+
} else {
3165+
num = (this / 100) * value.parseFloat();
3166+
return decimals !== undefined ? num.floor(decimals) : num;
3167+
}
3168+
3169+
} else
3170+
num = value.parseFloat();
3171+
3172+
switch (first) {
3173+
case 42:
3174+
num = this * num;
3175+
break;
3176+
case 43:
3177+
num = this + num;
3178+
break;
3179+
case 45:
3180+
num = this - num;
3181+
break;
3182+
case 47:
3183+
num = this / num;
3184+
break;
3185+
case 47:
3186+
num = this / num;
3187+
break;
3188+
default:
3189+
num = this;
3190+
break;
3191+
}
3192+
3193+
if (decimals !== undefined)
3194+
return num.floor(decimals);
3195+
3196+
return num;
3197+
};
3198+
31333199
/*
31343200
Format number :: 10000 = 10 000
31353201
@format {Number or String} :: number is decimal and string is specified format, example: ## ###.##
@@ -4665,4 +4731,4 @@ exports.minifyHTML = function(value) {
46654731

46664732
global.async = exports.async;
46674733
global.sync = global.SYNCHRONIZE = exports.sync;
4668-
global.sync2 = exports.sync2;
4734+
global.sync2 = exports.sync2;

0 commit comments

Comments
 (0)