@tim-code/my-util 0.4.5 → 0.4.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 +1 -1
- package/src/math.js +18 -0
- package/src/math.test.js +45 -1
package/package.json
CHANGED
package/src/math.js
CHANGED
|
@@ -123,3 +123,21 @@ export function formatPlus(number, { zero = false } = {}) {
|
|
|
123
123
|
}
|
|
124
124
|
return undefined
|
|
125
125
|
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Create an array of numbers progressing from start up to, but not including, end.
|
|
129
|
+
* @param {number} start
|
|
130
|
+
* @param {number=} end
|
|
131
|
+
* @param {number=} step
|
|
132
|
+
* @returns {number[]}
|
|
133
|
+
*/
|
|
134
|
+
export function range(start, end, increment = 1) {
|
|
135
|
+
if (!(increment > 0)) {
|
|
136
|
+
return []
|
|
137
|
+
}
|
|
138
|
+
const results = []
|
|
139
|
+
for (let i = start; i < end; i += increment) {
|
|
140
|
+
results.push(i)
|
|
141
|
+
}
|
|
142
|
+
return results
|
|
143
|
+
}
|
package/src/math.test.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { describe, expect, it } from "@jest/globals"
|
|
2
|
-
const { mod, formatPlus, line, sum, average, variance } = await import("./math.js")
|
|
2
|
+
const { mod, formatPlus, line, sum, average, variance, range } = await import("./math.js")
|
|
3
3
|
|
|
4
4
|
describe("mod", () => {
|
|
5
5
|
it("returns n when n is less than m and n is non-negative", () => {
|
|
@@ -255,3 +255,47 @@ describe("formatPlus", () => {
|
|
|
255
255
|
expect(formatPlus(NaN)).toBeUndefined()
|
|
256
256
|
})
|
|
257
257
|
})
|
|
258
|
+
|
|
259
|
+
describe("range", () => {
|
|
260
|
+
it("returns an empty array if start >= end", () => {
|
|
261
|
+
expect(range(5, 5)).toEqual([])
|
|
262
|
+
expect(range(10, 5)).toEqual([])
|
|
263
|
+
})
|
|
264
|
+
|
|
265
|
+
it("returns a sequence from start to end-1 with default increment 1", () => {
|
|
266
|
+
expect(range(0, 3)).toEqual([0, 1, 2])
|
|
267
|
+
expect(range(2, 6)).toEqual([2, 3, 4, 5])
|
|
268
|
+
})
|
|
269
|
+
|
|
270
|
+
it("returns a sequence with a custom positive increment", () => {
|
|
271
|
+
expect(range(0, 5, 2)).toEqual([0, 2, 4])
|
|
272
|
+
expect(range(1, 8, 3)).toEqual([1, 4, 7])
|
|
273
|
+
})
|
|
274
|
+
it("works with negative start and end", () => {
|
|
275
|
+
expect(range(-3, 1)).toEqual([-3, -2, -1, 0])
|
|
276
|
+
expect(range(-5, -2)).toEqual([-5, -4, -3])
|
|
277
|
+
})
|
|
278
|
+
|
|
279
|
+
it("returns an empty array if increment is zero", () => {
|
|
280
|
+
expect(range(0, 5, 0)).toEqual([])
|
|
281
|
+
})
|
|
282
|
+
|
|
283
|
+
it("returns an empty array if increment is negative and start < end", () => {
|
|
284
|
+
expect(range(0, 5, -1)).toEqual([])
|
|
285
|
+
expect(range(2, 6, -2)).toEqual([])
|
|
286
|
+
})
|
|
287
|
+
|
|
288
|
+
it("returns an empty array if increment is negative and start > end", () => {
|
|
289
|
+
expect(range(5, 0, -1)).toEqual([])
|
|
290
|
+
expect(range(3, -2, -2)).toEqual([])
|
|
291
|
+
})
|
|
292
|
+
|
|
293
|
+
it("returns an empty array if increment is positive and start > end", () => {
|
|
294
|
+
expect(range(5, 0, 1)).toEqual([])
|
|
295
|
+
expect(range(2, -2, 2)).toEqual([])
|
|
296
|
+
})
|
|
297
|
+
|
|
298
|
+
it("handles floating point increments", () => {
|
|
299
|
+
expect(range(0, 1, 0.25)).toEqual([0, 0.25, 0.5, 0.75])
|
|
300
|
+
})
|
|
301
|
+
})
|