@tim-code/my-util 0.6.5 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tim-code/my-util",
3
- "version": "0.6.5",
3
+ "version": "0.6.6",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "author": "Tim Sprowl",
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"`.
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)