@tim-code/my-util 0.6.4 → 0.6.6
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/package.json +2 -2
- package/src/math.js +19 -1
- package/src/math.test.js +47 -0
- package/src/object.js +1 -1
- package/src/run.js +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tim-code/my-util",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.6",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": "Tim Sprowl",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
25
|
"@jest/globals": "^30.2.0",
|
|
26
|
-
"@tim-code/eslint-config": "^2.0
|
|
26
|
+
"@tim-code/eslint-config": "^2.1.0",
|
|
27
27
|
"jest": "^30.2.0"
|
|
28
28
|
},
|
|
29
29
|
"jest": {
|
package/src/math.js
CHANGED
|
@@ -144,6 +144,24 @@ export function range(start, end, increment = 1) {
|
|
|
144
144
|
return results
|
|
145
145
|
}
|
|
146
146
|
|
|
147
|
+
/**
|
|
148
|
+
* Create an array of numbers progressing from start up to and including end.
|
|
149
|
+
* @param {number} start
|
|
150
|
+
* @param {number=} end
|
|
151
|
+
* @param {number=} increment
|
|
152
|
+
* @returns {number[]}
|
|
153
|
+
*/
|
|
154
|
+
export function between(start, end, increment = 1) {
|
|
155
|
+
if (!(increment > 0)) {
|
|
156
|
+
return []
|
|
157
|
+
}
|
|
158
|
+
const results = []
|
|
159
|
+
for (let i = start; i <= end; i += increment) {
|
|
160
|
+
results.push(i)
|
|
161
|
+
}
|
|
162
|
+
return results
|
|
163
|
+
}
|
|
164
|
+
|
|
147
165
|
/**
|
|
148
166
|
* Check if the argument is a number.
|
|
149
167
|
* This excludes Infinity and NaN, but otherwise is equivalent to `typeof number === "number"`.
|
|
@@ -176,7 +194,7 @@ export function quantiles(array, { N, key, method = Math.round, labeller = Math.
|
|
|
176
194
|
return undefined
|
|
177
195
|
}
|
|
178
196
|
const sorted = [...array].sort(ascending(key))
|
|
179
|
-
const result =
|
|
197
|
+
const result = Object.create(null)
|
|
180
198
|
for (let i = 0; i <= N; i++) {
|
|
181
199
|
const percentile = i / N
|
|
182
200
|
const percentileIndex = method(percentile * (sorted.length - 1))
|
package/src/math.test.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { describe, expect, it } from "@jest/globals"
|
|
2
|
+
import { between } from "./math.js"
|
|
2
3
|
|
|
3
4
|
// EXPORTED FUNCTIONS UNDER TEST:
|
|
4
5
|
// - mod
|
|
@@ -314,6 +315,52 @@ describe("range", () => {
|
|
|
314
315
|
})
|
|
315
316
|
})
|
|
316
317
|
|
|
318
|
+
describe("between", () => {
|
|
319
|
+
it("returns an empty array if start >= end", () => {
|
|
320
|
+
expect(between(7, 5)).toEqual([])
|
|
321
|
+
expect(between(10, 5)).toEqual([])
|
|
322
|
+
|
|
323
|
+
expect(between(5, 5)).toEqual([5])
|
|
324
|
+
})
|
|
325
|
+
|
|
326
|
+
it("returns a sequence from start to end with default increment 1", () => {
|
|
327
|
+
expect(between(0, 3)).toEqual([0, 1, 2, 3])
|
|
328
|
+
expect(between(2, 6)).toEqual([2, 3, 4, 5, 6])
|
|
329
|
+
})
|
|
330
|
+
|
|
331
|
+
it("returns a sequence with a custom positive increment", () => {
|
|
332
|
+
expect(between(0, 5, 2)).toEqual([0, 2, 4])
|
|
333
|
+
expect(between(1, 8, 3)).toEqual([1, 4, 7])
|
|
334
|
+
})
|
|
335
|
+
it("works with negative start and end", () => {
|
|
336
|
+
expect(between(-3, 1)).toEqual([-3, -2, -1, 0, 1])
|
|
337
|
+
expect(between(-5, -2)).toEqual([-5, -4, -3, -2])
|
|
338
|
+
})
|
|
339
|
+
|
|
340
|
+
it("returns an empty array if increment is zero", () => {
|
|
341
|
+
expect(between(0, 5, 0)).toEqual([])
|
|
342
|
+
})
|
|
343
|
+
|
|
344
|
+
it("returns an empty array if increment is negative and start < end", () => {
|
|
345
|
+
expect(between(0, 5, -1)).toEqual([])
|
|
346
|
+
expect(between(2, 6, -2)).toEqual([])
|
|
347
|
+
})
|
|
348
|
+
|
|
349
|
+
it("returns an empty array if increment is negative and start > end", () => {
|
|
350
|
+
expect(between(5, 0, -1)).toEqual([])
|
|
351
|
+
expect(between(3, -2, -2)).toEqual([])
|
|
352
|
+
})
|
|
353
|
+
|
|
354
|
+
it("returns an empty array if increment is positive and start > end", () => {
|
|
355
|
+
expect(between(5, 0, 1)).toEqual([])
|
|
356
|
+
expect(between(2, -2, 2)).toEqual([])
|
|
357
|
+
})
|
|
358
|
+
|
|
359
|
+
it("handles floating point increments", () => {
|
|
360
|
+
expect(between(0, 1, 0.25)).toEqual([0, 0.25, 0.5, 0.75, 1])
|
|
361
|
+
})
|
|
362
|
+
})
|
|
363
|
+
|
|
317
364
|
describe("isNumber", () => {
|
|
318
365
|
it("returns true for finite numbers", () => {
|
|
319
366
|
expect(isNumber(0)).toBe(true)
|
package/src/object.js
CHANGED
|
@@ -15,7 +15,7 @@ export function isObject(thing) {
|
|
|
15
15
|
* @returns {Object}
|
|
16
16
|
*/
|
|
17
17
|
export function mapValues(object, callback) {
|
|
18
|
-
const result =
|
|
18
|
+
const result = Object.create(null)
|
|
19
19
|
const keys = Object.keys(object)
|
|
20
20
|
for (const key of keys) {
|
|
21
21
|
result[key] = callback(object[key], key, object)
|
package/src/run.js
CHANGED
|
@@ -10,7 +10,7 @@ import { pathToFileURL } from "url"
|
|
|
10
10
|
export function runnable(importMetaUrl, args, callback) {
|
|
11
11
|
if (importMetaUrl === pathToFileURL(process.argv[1]).href) {
|
|
12
12
|
// module was not imported but called directly
|
|
13
|
-
const parameter =
|
|
13
|
+
const parameter = Object.create(null)
|
|
14
14
|
for (let i = 0; i < args.length; i++) {
|
|
15
15
|
parameter[args[i]] = process.argv[i + 2]
|
|
16
16
|
}
|