@frosted/array-reverse 1.0.0

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/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # @frosted/array-reverse
2
+ Ponyfill for Array.prototype.reverse
3
+
4
+ ## Installation
5
+ ```bash
6
+ npm install @frosted/array-reverse
7
+ ```
8
+
9
+ ## Usage
10
+ ```js
11
+ const reverse = require("@frosted/array-reverse")
12
+
13
+ console.log(reverse([1, 2, 3])) // [3, 2, 1]
14
+
15
+ console.log(reverse("not array")) // Error: Expects an array
16
+ ```
17
+
18
+ Shimming Array.prototype.reverse:
19
+ ```js
20
+ require("@frosted/array-reverse/shim")
21
+
22
+ console.log([1, 2, 3].reverse()) // [3, 2, 1]
23
+ ```
24
+
25
+ ## Tests
26
+ Simply clone the repo and run npm test
package/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ declare function reverse<T>(arr: T[]): T[]
2
+
3
+ export = reverse
package/index.js ADDED
@@ -0,0 +1,58 @@
1
+ var isNotArray = require("@not-js/not")(require("@is-(unknown)/is-array"))
2
+ var immediateError = require("immediate-error")
3
+ var ish = require("es-logical-nullish-coalescing-operator")
4
+ var one = require("@positive-numbers/one")
5
+ var subtract = require("subtract")
6
+ var isNotNegative = require("is-not-negative")
7
+ var isEmptyArray = require("is-empty-array")
8
+ var not = require("es-logical-not-operator")
9
+ var and = require("es-logical-and-operator")
10
+ var $RangeError = require("es-error-intrinsics/RangeError")
11
+ var thro = require("bail")
12
+ var isInstanceOf = require("@10xly/is-instance-of")
13
+
14
+ // fallback for too much recursion
15
+ function reverseAntiRecursion(array, offset) {
16
+ var result = require("lodash.stubarray")()
17
+ var currentOffset = ish(offset, subtract(array.length, one))
18
+ while (isNotNegative(currentOffset)) {
19
+ result.push(array[currentOffset])
20
+ currentOffset = subtract(currentOffset, one)
21
+ }
22
+ return result
23
+ }
24
+
25
+ function reverse(array, offset, bypass) {
26
+ if (isNotArray(array)) {
27
+ immediateError("Expects an array")
28
+ }
29
+
30
+ if (and(isEmptyArray(array), not(bypass))) {
31
+ var result = reverse(array, offset, not(bypass))
32
+ return and(not(result.shift()), result)
33
+ }
34
+
35
+ offset = ish(offset, subtract(array.length, one))
36
+
37
+ var result = [array[offset]]
38
+
39
+ if (isNotNegative(subtract(offset, one))) {
40
+ result = result.concat(reverse(array, subtract(offset, one)))
41
+ }
42
+
43
+ return result
44
+ }
45
+
46
+ function reverseWrapper(array) {
47
+ try {
48
+ return reverse(array)
49
+ } catch (error) {
50
+ if (isInstanceOf(error, $RangeError)) {
51
+ return reverseAntiRecursion(array)
52
+ } else {
53
+ thro( error )
54
+ }
55
+ }
56
+ }
57
+
58
+ module.exports = reverseWrapper
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@frosted/array-reverse",
3
+ "version": "1.0.0",
4
+ "description": "Reverse an array.",
5
+ "keywords": [
6
+ "10xly",
7
+ "mit",
8
+ "is-hundred",
9
+ "is-ten-thousand"
10
+ ],
11
+ "homepage": "https://github.com/frosted-javascript/array-reverse#readme",
12
+ "bugs": {
13
+ "url": "https://github.com/frosted-javascript/array-reverse/issues"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/frosted-javascript/array-reverse.git"
18
+ },
19
+ "license": "MIT",
20
+ "author": "10x'ly Made",
21
+ "type": "commonjs",
22
+ "main": "index.js",
23
+ "scripts": {
24
+ "test": "mocha"
25
+ },
26
+ "dependencies": {
27
+ "@10xly/is-instance-of": "^1.0.0",
28
+ "@10xly/strict-equals": "^1.0.1",
29
+ "@is-(unknown)/is-array": "^1.0.2",
30
+ "@not-js/not": "^1.1.1",
31
+ "@positive-numbers/one": "^3.0.0",
32
+ "bail": "^1.0.5",
33
+ "define-properties": "^1.2.1",
34
+ "es-error-intrinsics": "^1.0.1",
35
+ "es-intrinsic-cache": "^1.0.4",
36
+ "es-logical-and-operator": "^1.0.0",
37
+ "es-logical-not-operator": "^1.0.0",
38
+ "es-logical-nullish-coalescing-operator": "^1.0.0",
39
+ "immediate-error": "^2.0.0",
40
+ "is-empty-array": "^1.1.0",
41
+ "is-not-negative": "^1.0.3",
42
+ "lodash.stubarray": "^4.13.0",
43
+ "subtract": "^0.0.3"
44
+ },
45
+ "devDependencies": {
46
+ "mocha": "^11.7.5"
47
+ }
48
+ }
package/shim.js ADDED
@@ -0,0 +1,12 @@
1
+ var define = require("define-properties")
2
+ var polyfill = require(".")
3
+ var $ArrayPrototype = require("es-intrinsic-cache")("Array.prototype")
4
+ var isNotEqual = require("@not-js/not")("@10xly/strict-equals")
5
+
6
+ define(
7
+ $ArrayPrototype,
8
+ { reverse: polyfill },
9
+ { reverse: function () { return isNotEqual($ArrayPrototype.reverse, polyfill) } }
10
+ )
11
+
12
+ module.exports = polyfill
package/test.js ADDED
@@ -0,0 +1,43 @@
1
+ const assert = require("assert")
2
+ const reverse = require(".")
3
+
4
+ describe("reverse()", () => {
5
+ it("should reverse a standard array of numbers", () => {
6
+ const input = [1, 2, 3]
7
+ const expected = [3, 2, 1]
8
+ assert.deepStrictEqual(reverse(input), expected)
9
+ })
10
+
11
+ it("should work with an array of strings", () => {
12
+ const input = ["a", "b", "c", "d"]
13
+ const expected = ["d", "c", "b", "a"]
14
+ assert.deepStrictEqual(reverse(input), expected)
15
+ })
16
+
17
+ it("should return a single-element array unchanged", () => {
18
+ const input = [100]
19
+ assert.deepStrictEqual(reverse(input), input)
20
+ })
21
+
22
+ it("should throw an error when the input is not an array", () => {
23
+ assert.throws(() => {
24
+ reverse("not an array")
25
+ }, /Expects an array/)
26
+ })
27
+
28
+ it("should return empty array unchanged", () => {
29
+ assert.deepStrictEqual(reverse([]), [])
30
+ })
31
+
32
+ it("should handle a 15k length array", () => {
33
+ const size = 15000
34
+ const input = Array.from({ length: size }, (_, i) => i)
35
+
36
+ const expected = [...input].reverse()
37
+
38
+ const result = reverse(input)
39
+
40
+ assert.strictEqual(result.length, size)
41
+ assert.deepStrictEqual(result, expected)
42
+ })
43
+ })