@shgysk8zer0/polyfills 0.3.0 → 0.3.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/math.js CHANGED
@@ -1,69 +1,74 @@
1
- (function() {
2
- 'use strict';
1
+ if (! Number.hasOwnProperty('isSafeInteger')) {
2
+ Number.MAX_SAFE_INTEGER = 2**53 -1;
3
+ Number.MIN_SAFE_INTEGER = -Number.MAX_SAFE_INTEGER;
4
+ Number.isSafeInteger = num => num <= Number.MAX_SAFE_INTEGER && num >= Number.MIN_SAFE_INTEGER;
5
+ }
3
6
 
4
- if (! Number.hasOwnProperty('isSafeInteger')) {
5
- Number.MAX_SAFE_INTEGER = 2**53 -1;
6
- Number.MIN_SAFE_INTEGER = -Number.MAX_SAFE_INTEGER;
7
- Number.isSafeInteger = num => num <= Number.MAX_SAFE_INTEGER && num >= Number.MIN_SAFE_INTEGER;
8
- }
7
+ if (! Number.hasOwnProperty('EPSILON')) {
8
+ Number.EPSILON = 2**-52;
9
+ }
9
10
 
10
- if (! Number.hasOwnProperty('EPSILON')) {
11
- Number.EPSILON = 2**-52;
12
- }
11
+ if (! Math.hasOwnProperty('sign')) {
12
+ Math.sign = x => ((x > 0) - (x < 0)) || +x;
13
+ }
13
14
 
14
- if (! Math.hasOwnProperty('sign')) {
15
- Math.sign = x => ((x > 0) - (x < 0)) || +x;
16
- }
15
+ if (! Math.hasOwnProperty('trunc')) {
16
+ Math.trunc = x => {
17
+ const n = x - x%1;
18
+ return n===0 && (x<0 || (x===0 && (1/x !== 1/0))) ? -0 : n;
19
+ };
20
+ }
17
21
 
18
- if (! Math.hasOwnProperty('trunc')) {
19
- Math.trunc = x => {
20
- const n = x - x%1;
21
- return n===0 && (x<0 || (x===0 && (1/x !== 1/0))) ? -0 : n;
22
- };
23
- }
22
+ if (! Math.hasOwnProperty('expm1')) {
23
+ Math.expm1 = x => Math.exp(x) - 1;
24
+ }
24
25
 
25
- if (! Math.hasOwnProperty('expm1')) {
26
- Math.expm1 = x => Math.exp(x) - 1;
27
- }
26
+ if (! Math.hasOwnProperty('hypot')) {
27
+ Math.hypot = (...nums) => Math.sqrt(nums.reduce((sum, num) => sum + num**2, 0));
28
+ }
28
29
 
29
- if (! Math.hasOwnProperty('hypot')) {
30
- Math.hypot = (...nums) => Math.sqrt(nums.reduce((sum, num) => sum + num**2, 0));
31
- }
30
+ if (! Math.hasOwnProperty('cbrt')) {
31
+ Math.cbrt = x => x**(1/3);
32
+ }
32
33
 
33
- if (! Math.hasOwnProperty('cbrt')) {
34
- Math.cbrt = x => x**(1/3);
35
- }
34
+ if (! Math.hasOwnProperty('log10')) {
35
+ Math.log10 = x => Math.log(x) * Math.LOG10E;
36
+ }
36
37
 
37
- if (! Math.hasOwnProperty('log10')) {
38
- Math.log10 = x => Math.log(x) * Math.LOG10E;
39
- }
38
+ if (! Math.hasOwnProperty('log2')) {
39
+ Math.log2 = x => Math.log(x) * Math.LOG2E;
40
+ }
40
41
 
41
- if (! Math.hasOwnProperty('log2')) {
42
- Math.log2 = x => Math.log(x) * Math.LOG2E;
43
- }
42
+ if (! Math.hasOwnProperty('log1p')) {
43
+ Math.log1p = x => Math.log(1 + x);
44
+ }
44
45
 
45
- if (! Math.hasOwnProperty('log1p')) {
46
- Math.log1p = x => Math.log(1 + x);
47
- }
46
+ if (! Math.hasOwnProperty('fround')) {
47
+ Math.fround = (function (array) {
48
+ return function(x) {
49
+ return array[0] = x, array[0];
50
+ };
51
+ })(new Float32Array(1));
52
+ }
48
53
 
49
- if (! Math.hasOwnProperty('fround')) {
50
- Math.fround = (function (array) {
51
- return function(x) {
52
- return array[0] = x, array[0];
53
- };
54
- })(new Float32Array(1));
55
- }
54
+ if (! (Math.clamp instanceof Function)) {
55
+ Math.clamp = function(value, min, max) {
56
+ return Math.min(Math.max(value, min), max);
57
+ };
58
+ }
56
59
 
57
- if (! (Math.clamp instanceof Function)) {
58
- Math.clamp = function(value, min, max) {
59
- return Math.min(Math.max(value, min), max);
60
- };
61
- }
60
+ /*
61
+ * Question of if it will be `Math.clamp` or `Math.constrain`
62
+ */
63
+ if (! (Math.constrain instanceof Function)) {
64
+ Math.constrain = Math.clamp;
65
+ }
62
66
 
63
- /*
64
- * Question of if it will be `Math.clamp` or `Math.constrain`
65
- */
66
- if (! (Math.constrain instanceof Function)) {
67
- Math.constrain = Math.clamp;
68
- }
69
- })();
67
+ /**
68
+ * @see https://github.com/tc39/proposal-math-sum
69
+ */
70
+ if(! (Math.sum instanceof Function)) {
71
+ Math.sum = function(...nums) {
72
+ return nums.map(num => parseFloat(num)).reduce((sum, num) => sum + num);
73
+ };
74
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shgysk8zer0/polyfills",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "A collection of JavaScript polyfills",