@nejs/basic-extensions 2.21.0 → 2.21.5

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.
Files changed (36) hide show
  1. package/.idea/markdown.xml +8 -0
  2. package/.idea/modules.xml +8 -0
  3. package/.idea/ne-basic-extensions.iml +8 -0
  4. package/.idea/vcs.xml +6 -0
  5. package/CODE_STYLE.md +393 -0
  6. package/CODING_PHILOSOPHY.md +36 -0
  7. package/DOCUMENTATION_GUIDELINES.md +221 -0
  8. package/dist/@nejs/{basic-extensions.bundle.2.21.0.js → basic-extensions.bundle.2.21.5.js} +6 -6
  9. package/dist/@nejs/basic-extensions.bundle.2.21.5.js.map +7 -0
  10. package/dist/cjs/big.int.extension.js +153 -45
  11. package/dist/cjs/big.int.extension.js.map +1 -1
  12. package/dist/cjs/index.d.ts +1 -1
  13. package/dist/cjs/index.js +2 -0
  14. package/dist/cjs/index.js.map +1 -1
  15. package/dist/cjs/math.extension.d.ts +14 -0
  16. package/dist/cjs/math.extension.js +71 -0
  17. package/dist/cjs/math.extension.js.map +1 -0
  18. package/dist/cjs/number.extension.js +17 -0
  19. package/dist/cjs/number.extension.js.map +1 -1
  20. package/dist/mjs/big.int.extension.js +153 -45
  21. package/dist/mjs/big.int.extension.js.map +1 -1
  22. package/dist/mjs/index.d.ts +1 -1
  23. package/dist/mjs/index.js +2 -0
  24. package/dist/mjs/index.js.map +1 -1
  25. package/dist/mjs/math.extension.d.ts +14 -0
  26. package/dist/mjs/math.extension.js +68 -0
  27. package/dist/mjs/math.extension.js.map +1 -0
  28. package/dist/mjs/number.extension.js +17 -0
  29. package/dist/mjs/number.extension.js.map +1 -1
  30. package/package.json +2 -2
  31. package/repl.history +30 -30
  32. package/src/big.int.extension.js +171 -45
  33. package/src/index.js +2 -0
  34. package/src/math.extension.js +73 -0
  35. package/src/number.extension.js +18 -0
  36. package/dist/@nejs/basic-extensions.bundle.2.21.0.js.map +0 -7
@@ -15,52 +15,126 @@ import { Patch } from '@nejs/extension'
15
15
  * // Now the `BigInt` class has additional methods available
16
16
  */
17
17
  export const BigIntExtensions = new Patch(BigInt, {
18
- /**
19
- * Determines if the supplied `value` is a `BigInt`. This check is
20
- * performed by first checking the `typeof` the `value` and then
21
- * checking to see if the `value` is an `instanceof` `BigInt`
22
- *
23
- * @param {*} value The value that needs to be checked to determine
24
- * if it is a `BigInt` or not
25
- * @returns {boolean} `true` if the supplied `value` is a `BigInt`,
26
- * `false` otherwise
27
- *
28
- * @example
29
- * const bigInt = 1234567890123456789012345678901234567890n
30
- * isBigInt(bigInt) // true
31
- * isBigInt(1234567890123456789012345678901234567890) // false
32
- * isBigInt('1234567890123456789012345678901234567890') // false
33
- * isBigInt(BigInt('1234567890123456789012345678901234567890')) // true
34
- */
35
- isBigInt(value) {
36
- return typeof value === 'bigint' || value instanceof BigInt
37
- },
18
+ [Patch.kMutablyHidden]: {
19
+ /**
20
+ * Checks if all or some of the supplied values are numbers.
21
+ *
22
+ * This method uses the `Array.prototype.every` or `Array.prototype.some`
23
+ * method to check if all or some of the supplied values are numbers,
24
+ * respectively. The method to use is determined by the `which` parameter.
25
+ *
26
+ * @param {string} [which='every'] - Determines the method to use for the
27
+ * check. Can be either 'every' or 'some'. Defaults to 'every'.
28
+ * @param {...*} values - The values to check.
29
+ * @returns {boolean} - Returns `true` if all or some of the values are
30
+ * numbers (based on the `which` parameter), `false` otherwise.
31
+ *
32
+ * @example
33
+ * areNumbers('every', 1, 2, 3) // true
34
+ * areNumbers('some', 1, '2', 3) // true
35
+ * areNumbers('every', 1, '2', 3) // false
36
+ */
37
+ areBigInts(which = ['every', 'some'][0], ...values) {
38
+ if (which !== 'every' && which !== 'some') {
39
+ return false
40
+ }
38
41
 
39
- /**
40
- * Conditionally returns a value based on whether the supplied
41
- * `value` is a `BigInt` or not. If the `value` is a `BigInt`,
42
- * the `thenValue` will be returned. If it is not a `BigInt`,
43
- * the `elseValue` will be returned instead.
44
- *
45
- * @param {any} value The value to check to determine if it is a
46
- * `BigInt`
47
- * @param {any} thenValue The value to return if the supplied
48
- * `value` is a `BigInt`
49
- * @param {any} elseValue The value to return if the supplied
50
- * `value` is not a `BigInt`
51
- * @returns {any} Either the `thenValue` or `elseValue` depending
52
- * on if the supplied `value` is a `BigInt`
53
- *
54
- * @example
55
- * const bigInt = 1234567890123456789012345678901234567890n
56
- * const num = 42
57
- * ifBigInt(bigInt, 'is a BigInt', 'not a BigInt')
58
- * // 'is a BigInt'
59
- * ifBigInt(num, 'is a BigInt', 'not a BigInt')
60
- * // 'not a BigInt'
61
- */
62
- ifBigInt(value, thenValue, elseValue) {
63
- return isThenElse(this.isBigInt(value), thenValue, elseValue)
42
+ return values[which](num => this.isBigInt(num))
43
+ },
44
+
45
+ /**
46
+ * Determines if the supplied `value` is a `BigInt`. This check is
47
+ * performed by first checking the `typeof` the `value` and then
48
+ * checking to see if the `value` is an `instanceof` `BigInt`
49
+ *
50
+ * @param {*} value The value that needs to be checked to determine
51
+ * if it is a `BigInt` or not
52
+ * @returns {boolean} `true` if the supplied `value` is a `BigInt`,
53
+ * `false` otherwise
54
+ *
55
+ * @example
56
+ * const bigInt = 1234567890123456789012345678901234567890n
57
+ * isBigInt(bigInt) // true
58
+ * isBigInt(1234567890123456789012345678901234567890) // false
59
+ * isBigInt('1234567890123456789012345678901234567890') // false
60
+ * isBigInt(BigInt('1234567890123456789012345678901234567890')) // true
61
+ */
62
+ isBigInt(value) {
63
+ return typeof value === 'bigint' || value instanceof BigInt
64
+ },
65
+
66
+ /**
67
+ * Conditionally returns a value based on whether the supplied
68
+ * `value` is a `BigInt` or not. If the `value` is a `BigInt`,
69
+ * the `thenValue` will be returned. If it is not a `BigInt`,
70
+ * the `elseValue` will be returned instead.
71
+ *
72
+ * @param {any} value The value to check to determine if it is a
73
+ * `BigInt`
74
+ * @param {any} thenValue The value to return if the supplied
75
+ * `value` is a `BigInt`
76
+ * @param {any} elseValue The value to return if the supplied
77
+ * `value` is not a `BigInt`
78
+ * @returns {any} Either the `thenValue` or `elseValue` depending
79
+ * on if the supplied `value` is a `BigInt`
80
+ *
81
+ * @example
82
+ * const bigInt = 1234567890123456789012345678901234567890n
83
+ * const num = 42
84
+ * ifBigInt(bigInt, 'is a BigInt', 'not a BigInt')
85
+ * // 'is a BigInt'
86
+ * ifBigInt(num, 'is a BigInt', 'not a BigInt')
87
+ * // 'not a BigInt'
88
+ */
89
+ ifBigInt(value, thenValue, elseValue) {
90
+ return isThenElse(this.isBigInt(value), thenValue, elseValue)
91
+ },
92
+
93
+ /**
94
+ * The Math.min() static method returns the smallest of the numbers given
95
+ * as input parameters, or Infinity if there are no parameters.
96
+ *
97
+ * @param {bigint|number} values value1, …, valueN – Zero or more numbers
98
+ * among which the lowest value will be selected and returned.
99
+ * @returns {bigint|number|Infinity} The smallest of the given numbers.
100
+ * Returns Infinity if no parameters are provided.
101
+ */
102
+ min(...values) {
103
+ const sorter = (l,r) => l < r ? -1 : l > r ? 1 : 0
104
+
105
+ if (!values.length)
106
+ return Infinity
107
+
108
+ if (values.every(n => typeof n === 'bigint')) {
109
+ return values.toSorted(sorter).at(0)
110
+ }
111
+ else {
112
+ throw new TypeError('All supplied values must be of type bigint')
113
+ }
114
+ },
115
+
116
+ /**
117
+ * The Math.max() static method returns the largest of the numbers given
118
+ * as input parameters, or Infinity if there are no parameters.
119
+ *
120
+ * @param {bigint|number} values value1, …, valueN – Zero or more numbers
121
+ * among which the largest value will be selected and returned.
122
+ * @returns {bigint|number|Infinity} The largest of the given numbers.
123
+ * Returns Infinity if no parameters are provided.
124
+ */
125
+ max(...values) {
126
+ const sorter = (l,r) => l < r ? -1 : l > r ? 1 : 0
127
+
128
+ if (!values.length)
129
+ return Infinity
130
+
131
+ if (values.every(n => typeof n === 'bigint')) {
132
+ return values.toSorted(sorter).at(-1)
133
+ }
134
+ else {
135
+ throw new TypeError('All supplied values must be of type bigint')
136
+ }
137
+ },
64
138
  },
65
139
  })
66
140
 
@@ -81,6 +155,36 @@ const { isBigInt: pIsBigInt, ifBigInt: pIfBigInt } = BigIntExtensions.patches
81
155
  * // Now the `BigInt` prototype has additional methods available
82
156
  */
83
157
  export const BigIntPrototypeExtensions = new Patch(BigInt.prototype, {
158
+ /**
159
+ * Clamps a value between a minimum and maximum value.
160
+ *
161
+ * This method checks if the provided value and the min and max bounds are
162
+ * numbers. If they are not, it returns the original value. If they are,
163
+ * it ensures that the value does not go below the minimum value or above
164
+ * the maximum value.
165
+ *
166
+ * @param {bigint} [minValue=BigInt(-Number.MAX_VALUE)] - The minimum value.
167
+ * Defaults to BigInt(-Number.MAX_VALUE).
168
+ * @param {bigint} [maxValue=BigInt(Number.MAX_VALUE)] - The maximum value.
169
+ * Defaults to BigInt(Number.MAX_VALUE).
170
+ * @returns {bigint} - Returns the clamped value if all parameters are
171
+ * big integers.
172
+ *
173
+ * @example
174
+ * (10n).clamp(1n, 5n) // returns 5n
175
+ * (-10n).clamp(1n, 5n) // returns 1n
176
+ * (3n).clamp(1n, 5n) // returns 3n
177
+ */
178
+ clamp(
179
+ minValue = BigInt(-Number.MAX_VALUE),
180
+ maxValue = BigInt(Number.MAX_VALUE)
181
+ ) {
182
+ if (typeof minValue !== 'bigint' || typeof maxValue !== 'bigint')
183
+ throw new TypeError('All values must be big integers')
184
+
185
+ return BigInt.max(minValue, BigInt.min(maxValue, this))
186
+ },
187
+
84
188
  /**
85
189
  * A getter method that returns an object representation of the BigInt
86
190
  * instance.
@@ -149,6 +253,28 @@ export const BigIntPrototypeExtensions = new Patch(BigInt.prototype, {
149
253
  ifBigInt(thenValue, elseValue) {
150
254
  return pIfBigInt(this, thenValue, elseValue)
151
255
  },
256
+
257
+ /**
258
+ * Provides a way when dealing with numbers to determine if
259
+ * a given number is within a range of values. By default, if
260
+ * no parameters are supplied, it always returns true since
261
+ * the default range is -Infinity to +Infinity. Additionally,
262
+ * by default, the number will always be less than the supplied
263
+ * max unless inclusive is set to true.
264
+ *
265
+ * @param min the lower range value, defaults to -Infinity
266
+ * @param max the upper range value, defaults to +Infinity
267
+ * @param inclusive defaults to false, set to true if you
268
+ * want the max value to less than and equals to
269
+ * @returns {boolean} true if within the range, false otherwise
270
+ */
271
+ within(
272
+ min = BigInt(-Infinity),
273
+ max = BigInt(Infinity),
274
+ inclusive = false
275
+ ) {
276
+ return this >= min && (inclusive ? this <= max : this < max)
277
+ }
152
278
  })
153
279
 
154
280
  // NOTE to self; this is repeated here otherwise a circular reference from
package/src/index.js CHANGED
@@ -4,6 +4,7 @@ import { FunctionExtensions, FunctionPrototypeExtensions } from './function.exte
4
4
  import { GlobalFunctionsAndProps } from './global.this.js'
5
5
  import { JSONExtensions } from './json.extensions.js'
6
6
  import { MapExtensions, MapPrototypeExtensions } from './map.extensions.js'
7
+ import { MathExtensions } from './math.extension.js'
7
8
  import { NumberExtensions, NumberPrototypeExtensions } from './number.extension.js'
8
9
  import { ObjectExtensions, ObjectPrototypeExtensions } from './object.extensions.js'
9
10
  import { ReflectExtensions } from './reflect.extensions.js'
@@ -53,6 +54,7 @@ const StaticPatches = [
53
54
  [Function, FunctionExtensions, Function.name],
54
55
  [JSON, JSONExtensions, 'JSON'], // Missing a .name property
55
56
  [Map, MapExtensions, Map.name],
57
+ [Math, MathExtensions, 'Math'],
56
58
  [Number, NumberExtensions, Number.name],
57
59
  [Object, ObjectExtensions, Object.name],
58
60
  [Reflect, ReflectExtensions, 'Reflect'], // Missing a .name property
@@ -0,0 +1,73 @@
1
+ import { Patch } from '@nejs/extension'
2
+
3
+ /**
4
+ * `Math` extensions. Building better worlds...actually just
5
+ * providing some parity between Number and BigInt types for
6
+ * now, but later, better worlds.
7
+ *
8
+ * @type {Patch}
9
+ * @example
10
+ * import { MathExtensions } from 'math.extension.js'
11
+ *
12
+ * MathExtensions.apply()
13
+ * // Now the `Math` class has additional methods available
14
+ */
15
+ export const MathExtensions = new Patch(Math, {
16
+ [Patch.kMutablyHidden]: {
17
+ /**
18
+ * The Math.min() static method returns the smallest of the numbers given
19
+ * as input parameters, or Infinity if there are no parameters.
20
+ *
21
+ * @param {bigint|number} values value1, …, valueN – Zero or more numbers
22
+ * among which the lowest value will be selected and returned.
23
+ * @returns {bigint|number|NaN|Infinity} The smallest of the given numbers.
24
+ * Returns NaN if any of the parameters is or is converted into NaN or if
25
+ * the types of numbers are mismatched (i.e., bigint vs. number types).
26
+ * Returns Infinity if no parameters are provided.
27
+ */
28
+ min(...values) {
29
+ const sorter = (l,r) => l < r ? -1 : l > r ? 1 : 0
30
+
31
+ if (!values.length)
32
+ return Infinity
33
+
34
+ if (values.every(n => typeof n === 'bigint')) {
35
+ return values.toSorted(sorter).at(0)
36
+ }
37
+ else if (values.every(n => typeof n === 'number')) {
38
+ return values.toSorted(sorter).at(0)
39
+ }
40
+ else {
41
+ return NaN
42
+ }
43
+ },
44
+
45
+ /**
46
+ * The Math.max() static method returns the largest of the numbers given
47
+ * as input parameters, or Infinity if there are no parameters.
48
+ *
49
+ * @param {bigint|number} values value1, …, valueN – Zero or more numbers
50
+ * among which the largest value will be selected and returned.
51
+ * @returns {bigint|number|NaN|Infinity} The largest of the given numbers.
52
+ * Returns NaN if any of the parameters is or is converted into NaN or if
53
+ * the types of numbers are mismatched (i.e., bigint vs. number types).
54
+ * Returns Infinity if no parameters are provided.
55
+ */
56
+ max(...values) {
57
+ const sorter = (l,r) => l < r ? -1 : l > r ? 1 : 0
58
+
59
+ if (!values.length)
60
+ return Infinity
61
+
62
+ if (values.every(n => typeof n === 'bigint')) {
63
+ return values.toSorted(sorter).at(-1)
64
+ }
65
+ else if (values.every(n => typeof n === 'number')) {
66
+ return values.toSorted(sorter).at(-1)
67
+ }
68
+ else {
69
+ return NaN
70
+ }
71
+ },
72
+ },
73
+ })
@@ -258,6 +258,24 @@ export const NumberPrototypeExtensions = new Patch(Number.prototype, {
258
258
  ifNumber(thenValue, elseValue) {
259
259
  return pIfNumber(this, thenValue, elseValue)
260
260
  },
261
+
262
+ /**
263
+ * Provides a way when dealing with numbers to determine if
264
+ * a given number is within a range of values. By default, if
265
+ * no parameters are supplied, it always returns true since
266
+ * the default range is -Infinity to +Infinity. Additionally,
267
+ * by default, the number will always be less than the supplied
268
+ * max unless inclusive is set to true.
269
+ *
270
+ * @param min the lower range value, defaults to -Infinity
271
+ * @param max the upper range value, defaults to +Infinity
272
+ * @param inclusive defaults to false, set to true if you
273
+ * want the max value to less than and equals to
274
+ * @returns {boolean} true if within the range, false otherwise
275
+ */
276
+ within(min = -Infinity, max = Infinity, inclusive = false) {
277
+ return this >= min && (inclusive ? this <= max : this < max)
278
+ }
261
279
  }
262
280
  })
263
281