@frosted/array-at 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,28 @@
1
+ # @frosted/array-at
2
+ Ponyfill for Array.prototype.at
3
+
4
+ ## Installation
5
+ ```bash
6
+ npm install @frosted/array-at
7
+ ```
8
+
9
+ ## Usage
10
+ ```js
11
+ const at = require("@frosted/array-at")
12
+
13
+ console.log(at([1, 2, 3], 1)) // 2
14
+
15
+ console.log(at([20, 40, 60, 80], -2)) // 60
16
+
17
+ console.log(at("not array", 2)) // Error: Expects an array
18
+ ```
19
+
20
+ Shimming Array.prototype.at:
21
+ ```js
22
+ require("@frosted/array-at/shim")
23
+
24
+ console.log([1, 2, 3].at()) // [3, 2, 1]
25
+ ```
26
+
27
+ ## Tests
28
+ Simply clone the repo and run npm test
package/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ declare function at<T>(arr: T[], index: number): T
2
+
3
+ export = at
package/index.js ADDED
@@ -0,0 +1,20 @@
1
+ var isNotArray = require("@not-js/not")(require("@is-(unknown)/is-array"))
2
+ var immediateError = require("immediate-error")
3
+ var reverse = require("@frosted/array-reverse")
4
+ var isNegative = require("is-negative")
5
+ var abs = require("lolite.abs")
6
+ var addOne = require("add-one")
7
+
8
+ function at(array, index) {
9
+ if (isNotArray(array)) {
10
+ immediateError("Expects an array")
11
+ }
12
+
13
+ if (isNegative(index)) {
14
+ return at(reverse(array), abs(addOne(index)))
15
+ }
16
+
17
+ return array[index]
18
+ }
19
+
20
+ module.exports = at
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@frosted/array-at",
3
+ "version": "1.0.0",
4
+ "description": "Get the item at a specified index in an array, allowing for positive and negative integers",
5
+ "keywords": [
6
+ "10xly",
7
+ "made",
8
+ "is-hundred",
9
+ "frosted-javascript"
10
+ ],
11
+ "homepage": "https://github.com/frosted-javascript/array-at#readme",
12
+ "bugs": {
13
+ "url": "https://github.com/frosted-javascript/array-at/issues"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/frosted-javascript/array-at.git"
18
+ },
19
+ "license": "MIT",
20
+ "author": "10x'ly Made / Frosted",
21
+ "type": "commonjs",
22
+ "main": "index.js",
23
+ "scripts": {
24
+ "test": "mocha"
25
+ },
26
+ "dependencies": {
27
+ "@10xly/strict-equals": "^1.0.1",
28
+ "@frosted/array-reverse": "^1.0.0",
29
+ "@is-(unknown)/is-array": "^1.0.2",
30
+ "@not-js/not": "^1.1.1",
31
+ "add-one": "^1.0.3",
32
+ "define-properties": "^1.2.1",
33
+ "immediate-error": "^2.0.0",
34
+ "is-negative": "^2.1.0",
35
+ "lolite.abs": "^1.1.17",
36
+ "ununcurry-this-x": "^1.0.0"
37
+ },
38
+ "devDependencies": {
39
+ "mocha": "^11.7.5"
40
+ }
41
+ }
package/shim.js ADDED
@@ -0,0 +1,12 @@
1
+ var define = require("define-properties")
2
+ var polyfill = require("ununcurry-this-x")(require("."))
3
+ var $ArrayPrototype = require("es-intrinsic-cache")("Array.prototype")
4
+ var isNotEqual = require("@not-js/not")(require("@10xly/strict-equals"))
5
+
6
+ define(
7
+ $ArrayPrototype,
8
+ { at: polyfill },
9
+ { at: function () { return isNotEqual($ArrayPrototype.at, polyfill) } }
10
+ )
11
+
12
+ module.exports = polyfill
package/test.js ADDED
@@ -0,0 +1,33 @@
1
+ const assert = require("assert")
2
+ const at = require(".")
3
+
4
+ describe("at()", () => {
5
+ it("should return the last element when index is -1", () => {
6
+ const input = [10, 20, 30]
7
+ assert.strictEqual(at(input, -1), 30)
8
+ })
9
+
10
+ it("should return the second to last element when index is -2", () => {
11
+ const input = ["apple", "banana", "cherry"]
12
+ assert.strictEqual(at(input, -2), "banana")
13
+ })
14
+
15
+ it("should return the first element when index is 0", () => {
16
+ const input = ["first", "second"]
17
+ assert.strictEqual(at(input, 0), "first")
18
+ })
19
+
20
+ it("should return undefined for out-of-bounds negative index", () => {
21
+ const input = [1, 2]
22
+ assert.strictEqual(at(input, -3), undefined)
23
+ })
24
+
25
+ it("should throw for non-array input", () => {
26
+ assert.throws(() => at(null, 0), /Expects an array/)
27
+ })
28
+
29
+ it("should handle large arrays", () => {
30
+ const bigArray = Array.from({ length: 15000 }, (_, i) => i)
31
+ assert.strictEqual(at(bigArray, -1), 14999)
32
+ })
33
+ })