├── .githooks
└── pre-commit
├── .github
├── release.yml
└── workflows
│ └── test.yml
├── .gitignore
├── LICENSE
├── README.md
├── lerna.json
├── package.json
├── packages
├── benchmark
│ ├── CHANGELOG.md
│ ├── README.md
│ ├── package.json
│ └── src
│ │ ├── array.js
│ │ ├── immutable-array.js
│ │ └── util.js
├── copy-within
│ ├── CHANGELOG.md
│ ├── LICENSE
│ ├── README.md
│ ├── jest.config.js
│ ├── package.json
│ ├── src
│ │ └── copy-within.ts
│ ├── test
│ │ └── copy-within-test.ts
│ └── tsconfig.json
├── fill
│ ├── CHANGELOG.md
│ ├── LICENSE
│ ├── README.md
│ ├── package.json
│ ├── src
│ │ └── fill.ts
│ ├── test
│ │ └── fill-test.ts
│ └── tsconfig.json
├── pop
│ ├── CHANGELOG.md
│ ├── LICENSE
│ ├── README.md
│ ├── jest.config.js
│ ├── package.json
│ ├── src
│ │ └── pop.ts
│ ├── test
│ │ └── pop-test.ts
│ └── tsconfig.json
├── prototype
│ ├── CHANGELOG.md
│ ├── LICENSE
│ ├── README.md
│ ├── jest.config.js
│ ├── package.json
│ ├── src
│ │ └── prototype.ts
│ ├── test
│ │ └── prototype-test.ts
│ └── tsconfig.json
├── push
│ ├── CHANGELOG.md
│ ├── LICENSE
│ ├── README.md
│ ├── jest.config.js
│ ├── package.json
│ ├── src
│ │ └── push.ts
│ ├── test
│ │ └── push-test.ts
│ └── tsconfig.json
├── reverse
│ ├── CHANGELOG.md
│ ├── LICENSE
│ ├── README.md
│ ├── jest.config.js
│ ├── package.json
│ ├── src
│ │ └── reverse.ts
│ ├── test
│ │ └── reverse-test.ts
│ └── tsconfig.json
├── shift
│ ├── CHANGELOG.md
│ ├── LICENSE
│ ├── README.md
│ ├── jest.config.js
│ ├── package.json
│ ├── src
│ │ └── shift.ts
│ ├── test
│ │ └── shift-test.ts
│ └── tsconfig.json
├── sort
│ ├── CHANGELOG.md
│ ├── LICENSE
│ ├── README.md
│ ├── package.json
│ ├── src
│ │ └── sort.ts
│ ├── test
│ │ └── sort-test.ts
│ └── tsconfig.json
├── splice
│ ├── CHANGELOG.md
│ ├── LICENSE
│ ├── README.md
│ ├── jest.config.js
│ ├── package.json
│ ├── src
│ │ └── splice.ts
│ ├── test
│ │ └── splice-test.ts
│ └── tsconfig.json
└── unshift
│ ├── CHANGELOG.md
│ ├── LICENSE
│ ├── README.md
│ ├── jest.config.js
│ ├── package.json
│ ├── src
│ └── unshift.ts
│ ├── test
│ └── unshift-test.ts
│ └── tsconfig.json
├── prettier.config.js
├── tsconfig.json
└── yarn.lock
/.githooks/pre-commit:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | npx --no-install lint-staged
3 |
--------------------------------------------------------------------------------
/.github/release.yml:
--------------------------------------------------------------------------------
1 | changelog:
2 | exclude:
3 | labels:
4 | - 'Type: Meta'
5 | - 'Type: Question'
6 | - 'Type: Release'
7 |
8 | categories:
9 | - title: Security Fixes
10 | labels: ['Type: Security']
11 | - title: Breaking Changes
12 | labels: ['Type: Breaking Change']
13 | - title: Features
14 | labels: ['Type: Feature']
15 | - title: Bug Fixes
16 | labels: ['Type: Bug']
17 | - title: Documentation
18 | labels: ['Type: Documentation']
19 | - title: Refactoring
20 | labels: ['Type: Refactoring']
21 | - title: Testing
22 | labels: ['Type: Testing']
23 | - title: Maintenance
24 | labels: ['Type: Maintenance']
25 | - title: CI
26 | labels: ['Type: CI']
27 | - title: Dependency Updates
28 | labels: ['Type: Dependencies', "dependencies"]
29 | - title: Other Changes
30 | labels: ['*']
31 |
--------------------------------------------------------------------------------
/.github/workflows/test.yml:
--------------------------------------------------------------------------------
1 | name: test
2 | on: [push, pull_request]
3 | jobs:
4 | test:
5 | name: "Test on Node.js ${{ matrix.node-version }}"
6 | runs-on: ubuntu-latest
7 | strategy:
8 | matrix:
9 | node-version: [14, 16, 18]
10 | steps:
11 | - name: checkout
12 | uses: actions/checkout@v3
13 | - name: setup Node.js ${{ matrix.node-version }}
14 | uses: actions/setup-node@v3
15 | with:
16 | node-version: ${{ matrix.node-version }}
17 | - name: Install
18 | run: yarn install
19 | - name: Test
20 | run: yarn test
21 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ### https://raw.github.com/github/gitignore/608690d6b9a78c2a003affc792e49a84905b3118/Node.gitignore
2 |
3 | # Logs
4 | logs
5 | *.log
6 |
7 | # Runtime data
8 | pids
9 | *.pid
10 | *.seed
11 |
12 | # Directory for instrumented libs generated by jscoverage/JSCover
13 | lib-cov
14 |
15 | # Coverage directory used by tools like istanbul
16 | coverage
17 |
18 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
19 | .grunt
20 |
21 | # node-waf configuration
22 | .lock-wscript
23 |
24 | # Compiled binary addons (http://nodejs.org/api/addons.html)
25 | build/Release
26 |
27 | # Dependency directory
28 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
29 | node_modules
30 |
31 | # Debug log from npm
32 | npm-debug.log
33 |
34 |
35 | ### https://raw.github.com/github/gitignore/608690d6b9a78c2a003affc792e49a84905b3118/Global/JetBrains.gitignore
36 |
37 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm
38 |
39 | *.iml
40 |
41 | ## Directory-based project format:
42 | .idea/
43 | # if you remove the above rule, at least ignore the following:
44 |
45 | # User-specific stuff:
46 | # .idea/workspace.xml
47 | # .idea/tasks.xml
48 | # .idea/dictionaries
49 |
50 | # Sensitive or high-churn files:
51 | # .idea/dataSources.ids
52 | # .idea/dataSources.xml
53 | # .idea/sqlDataSources.xml
54 | # .idea/dynamic.xml
55 | # .idea/uiDesigner.xml
56 |
57 | # Gradle:
58 | # .idea/gradle.xml
59 | # .idea/libraries
60 |
61 | # Mongo Explorer plugin:
62 | # .idea/mongoSettings.xml
63 |
64 | ## File-based project format:
65 | *.ipr
66 | *.iws
67 |
68 | ## Plugin-specific files:
69 |
70 | # IntelliJ
71 | out/
72 |
73 | # mpeltonen/sbt-idea plugin
74 | .idea_modules/
75 |
76 | # JIRA plugin
77 | atlassian-ide-plugin.xml
78 |
79 | # Crashlytics plugin (for Android Studio and IntelliJ)
80 | com_crashlytics_export_strings.xml
81 | crashlytics.properties
82 | crashlytics-build.properties
83 |
84 | # ignore
85 | lib/
86 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2017 azu
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy
4 | of this software and associated documentation files (the "Software"), to deal
5 | in the Software without restriction, including without limitation the rights
6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | copies of the Software, and to permit persons to whom the Software is
8 | furnished to do so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in all
11 | copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 | SOFTWARE.
20 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # immutable-array-prototype [](https://github.com/azu/immutable-array-prototype/actions?query=workflow%3A"test")
2 |
3 | Immutable Array prototype methods.
4 |
5 | - TypeScript
6 | - Small and Thin
7 | - `@immutable-array/prototype` that includes all methods: ~500bytes(gzip+minify)
8 | - Per method packages
9 | - `@immutable-array/push`, `@immutable-array/pop` etc...
10 | - Same usage with native `Array.prototype` methods
11 |
12 | ## Why?
13 |
14 | ECMAScript `Array` has some mutable methods.
15 |
16 | This library provide immutable version of each methods.
17 |
18 | ### Mutable method on `Array.prototype`
19 |
20 | | Native method: Return type | `@immutable-array/*` |
21 | | ---------------------------------------- | ---------------------------------------- |
22 | | [`Array.prototype.pop()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/pop): `any`| [`pop()`](packages/pop): new `Array` |
23 | | [`Array.prototype.push()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/push): `Number`| [`push()`](packages/push): new `Array` |
24 | | [`Array.prototype.shift()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/shift): `any`| [`shift()`](packages/shift): new `Array` |
25 | | [`Array.prototype.unshift()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift): `Number`| [`unshift()`](packages/unshift): new `Array` |
26 | | [`Array.prototype.splice()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/splice): `Array`| [`splice()`](packages/splice): new `Array` |
27 | | [`Array.prototype.reverse()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse): `Array`| [`reverse()`](packages/sort): new `Array` |
28 | | [`Array.prototype.sort()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/sort): `Array`| [`sort()`](packages/sort): new `Array` |
29 | | [`Array.prototype.fill()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/fill): `Array`| [`fill()`](packages/fill): new `Array` |
30 | | [`Array.prototype.copyWithin()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/copyWithin): `Array`| [`copyWithin()`](packages/copy-within): new `Array` |
31 |
32 |
33 | ## Install
34 |
35 | `@immutable-array/prototype` includes all methods.
36 |
37 | Install with [npm](https://www.npmjs.com/):
38 |
39 | npm install @immutable-array/prototype
40 |
41 | If you want to a single method, you can use a method as a package.
42 |
43 | Per method packages:
44 |
45 | npm install @immutable-array/pop
46 | npm install @immutable-array/push
47 | npm install @immutable-array/shift
48 | npm install @immutable-array/unshift
49 | npm install @immutable-array/sort
50 | npm install @immutable-array/reverse
51 | npm install @immutable-array/fill
52 | npm install @immutable-array/splice
53 | npm install @immutable-array/copy-within
54 |
55 | See each [package's README](./packages/) for more details.
56 |
57 | ## Usage
58 |
59 | `@immutable-array/prototype` is a collection of immutable `Array.prototype` methods.
60 |
61 | Basically, the usage of these method is same with mutable version.
62 |
63 | ```js
64 | import {
65 | sort,
66 | unshift,
67 | push,
68 | fill,
69 | splice,
70 | pop,
71 | reverse,
72 | copyWithin,
73 | shift
74 | } from '@immutable-array/prototype';
75 | describe('prototype', () => {
76 | it('shift', () => {
77 | assert.deepStrictEqual(shift(['a', 'b', 'c', 'd', 'e']), [
78 | 'b',
79 | 'c',
80 | 'd',
81 | 'e'
82 | ]);
83 | });
84 | it('unshift', () => {
85 | assert.deepStrictEqual(unshift(['a', 'b', 'c', 'd', 'e'], 'x'), [
86 | 'x',
87 | 'a',
88 | 'b',
89 | 'c',
90 | 'd',
91 | 'e'
92 | ]);
93 | });
94 | it('pop', () => {
95 | assert.deepStrictEqual(pop(['a', 'b', 'c', 'd', 'e']), [
96 | 'a',
97 | 'b',
98 | 'c',
99 | 'd'
100 | ]);
101 | });
102 | it('push', () => {
103 | assert.deepStrictEqual(push(['a', 'b', 'c', 'd', 'e'], 'x'), [
104 | 'a',
105 | 'b',
106 | 'c',
107 | 'd',
108 | 'e',
109 | 'x'
110 | ]);
111 | });
112 | it('splice', () => {
113 | assert.deepStrictEqual(splice(['a', 'b', 'c', 'd', 'e'], 0, 1, 'x'), [
114 | 'x',
115 | 'b',
116 | 'c',
117 | 'd',
118 | 'e'
119 | ]);
120 | });
121 | it('sort', () => {
122 | assert.deepStrictEqual(sort(['e', 'a', 'c', 'b', 'd']), [
123 | 'a',
124 | 'b',
125 | 'c',
126 | 'd',
127 | 'e'
128 | ]);
129 | });
130 | it('reverse', () => {
131 | assert.deepStrictEqual(reverse(['a', 'b', 'c', 'd', 'e']), [
132 | 'e',
133 | 'd',
134 | 'c',
135 | 'b',
136 | 'a'
137 | ]);
138 | });
139 | it('fill', () => {
140 | assert.deepStrictEqual(fill(new Array(5), 'x'), ['x', 'x', 'x', 'x', 'x']);
141 | });
142 | it('copyWithin', () => {
143 | assert.deepStrictEqual(copyWithin(['a', 'b', 'c', 'd', 'e'], 0, 3, 4), [
144 | 'd',
145 | 'b',
146 | 'c',
147 | 'd',
148 | 'e'
149 | ]);
150 | });
151 | });
152 | ```
153 |
154 | ## Benchmarks
155 |
156 | Benchmark that is native `Array.prototype` methods vs. `@immutable-array`
157 |
158 | See [benchmark](packages/benchmark).
159 |
160 | Native `Array.prototype` | @immutable-array
161 | > node src/array.js | > immutable-array.js
162 | |
163 | # pop 200000 times >>> # pop 200000 times
164 | ok ~330 ms (0 s + 330397151 ns) >>> ok ~267 ms (0 s + 267348617 ns)
165 | |
166 | # push 200000 times >>> # push 200000 times
167 | ok ~169 ms (0 s + 168738061 ns) >>> ok ~141 ms (0 s + 140502324 ns)
168 | |
169 | # shift 200000 times <<< # shift 200000 times
170 | ok ~296 ms (0 s + 295892983 ns) <<< ok ~419 ms (0 s + 418852725 ns)
171 | |
172 | # unshift 200000 times <<< # unshift 200000 times
173 | ok ~51 ms (0 s + 50817590 ns) <<< ok ~191 ms (0 s + 191329502 ns)
174 | |
175 | # sort 2000 times >>> # sort 2000 times
176 | ok ~933 ms (0 s + 932551400 ns) >>> ok ~611 ms (0 s + 610748601 ns)
177 | |
178 | # reverse 200000 times >>> # reverse 200000 times
179 | ok ~555 ms (0 s + 554921645 ns) >>> ok ~455 ms (0 s + 455068191 ns)
180 | |
181 | # fill 200000 times >>> # fill 200000 times
182 | ok ~782 ms (0 s + 782159758 ns) >>> ok ~699 ms (0 s + 698677543 ns)
183 | |
184 | # splice 200000 times <<< # splice 200000 times
185 | ok ~287 ms (0 s + 286547242 ns) <<< ok ~391 ms (0 s + 391294720 ns)
186 | |
187 | # copyWithin 200000 times <<< # copyWithin 200000 times
188 | ok ~237 ms (0 s + 236837575 ns) <<< ok ~275 ms (0 s + 275267401 ns)
189 | |
190 | all benchmarks completed >>> all benchmarks completed
191 | ok ~3.64 s (3 s + 638863405 ns) >>> ok ~3.45 s (3 s + 449089624 ns)
192 |
193 | ## Support Policy
194 |
195 | ### Do
196 |
197 | - Provide immutable version of `Array.prototype` method
198 | - Provide each method as an module
199 | - e.g.) `import push from "@immutable-array/push"`
200 | - All prototype method: `import { push } from "@immutable-array/prototype"`
201 | - ECMAScript compatible API
202 |
203 | For example, `@immutable-array/*` method should return same result with native API.
204 |
205 | ```js
206 | import { splice } from '@immutable-array/splice';
207 | var array = [1, 2, 3];
208 | // immutable
209 | var resultArray = splice(array, -1, 1, 'x');
210 | // native
211 | array.splice(-1, 1, 'x');
212 | assert.deepStrictEqual(array, resultArray);
213 | ```
214 |
215 | ### Do not
216 |
217 | - Add non-standard method in ECMAScript
218 | - e.g.) `update`, `delete`, `merge`...
219 | - Each method depended on other method
220 |
221 | ## Related
222 |
223 | - [Pure javascript immutable arrays](https://vincent.billey.me/pure-javascript-immutable-array/ "Pure javascript immutable arrays")
224 | - [georapbox/immutable-arrays: Immutable versions of normally mutable array methods](https://github.com/georapbox/immutable-arrays "georapbox/immutable-arrays: Immutable versions of normally mutable array methods")
225 | - [micnews/immutable-array-methods](https://github.com/micnews/immutable-array-methods "micnews/immutable-array-methods")
226 | - [kolodny/immutability-helper: mutate a copy of data without changing the original source](https://github.com/kolodny/immutability-helper "kolodny/immutability-helper: mutate a copy of data without changing the original source")
227 |
228 | ## Changelog
229 |
230 | See [Releases page](https://github.com/azu/immutable-array-prototype/releases).
231 |
232 | ## Running tests
233 |
234 | Run following commands:
235 |
236 | yarn install
237 | yarn test
238 |
239 | ## Contributing
240 |
241 | Pull requests and stars are always welcome.
242 |
243 | For bugs and feature requests, [please create an issue](https://github.com/azu/immutable-array-prototype/issues).
244 |
245 | 1. Fork it!
246 | 2. Create your feature branch: `git checkout -b my-new-feature`
247 | 3. Commit your changes: `git commit -am 'Add some feature'`
248 | 4. Push to the branch: `git push origin my-new-feature`
249 | 5. Submit a pull request :D
250 |
251 | ## Author
252 |
253 | - [github/azu](https://github.com/azu)
254 | - [twitter/azu_re](https://twitter.com/azu_re)
255 |
256 | ## License
257 |
258 | MIT © azu
259 |
--------------------------------------------------------------------------------
/lerna.json:
--------------------------------------------------------------------------------
1 | {
2 | "lerna": "2.4.0",
3 | "packages": ["packages/*"],
4 | "version": "1.0.6",
5 | "npmClient": "yarn",
6 | "useWorkspaces": true
7 | }
8 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "private": true,
3 | "author": "azu",
4 | "license": "MIT",
5 | "name": "immutable-array-prototype",
6 | "version": "1.0.0",
7 | "description": "Immutable Array prototype methods.",
8 | "main": "lib/immutable-array-prototype.js",
9 | "scripts": {
10 | "test": "yarn run build && jest",
11 | "build": "lerna run build",
12 | "format": "prettier --write \"**/*.{js,jsx,ts,tsx,css}\"",
13 | "prepare": "git config --local core.hooksPath .githooks",
14 | "versionup": "lerna version",
15 | "ci:versionup:patch": "lerna version patch --no-push --no-git-tag-version --yes && npm run commit-version",
16 | "ci:versionup:minor": "lerna version minor --no-push --no-git-tag-version --yes && npm run commit-version",
17 | "ci:versionup:major": "lerna version major --no-push --no-git-tag-version --yes && npm run commit-version",
18 | "commit-version": "git add . && git commit -m \"chore(release): v`node -p 'require(\"./lerna.json\").version'`\"",
19 | "release": "lerna publish from-package",
20 | "ci:release": "lerna publish from-package --yes"
21 | },
22 | "workspaces": [
23 | "packages/*"
24 | ],
25 | "lint-staged": {
26 | "*.{js,jsx,ts,tsx,css}": [
27 | "prettier --write"
28 | ]
29 | },
30 | "jest": {
31 | "projects": [
32 | "packages/*"
33 | ],
34 | "testMatch": [
35 | "**/?(*.)(spec|test).ts?(x)"
36 | ]
37 | },
38 | "keywords": [
39 | "array",
40 | "immutable",
41 | "ecmascript",
42 | "ponyfill"
43 | ],
44 | "repository": {
45 | "type": "git",
46 | "url": "https://github.com/azu/immutable-array-prototype.git"
47 | },
48 | "bugs": {
49 | "url": "https://github.com/azu/immutable-array-prototype/issues"
50 | },
51 | "homepage": "https://github.com/azu/immutable-array-prototype",
52 | "devDependencies": {
53 | "@types/jest": "^29.2.5",
54 | "@types/node": "^18.11.18",
55 | "jest": "^29.3.1",
56 | "lerna": "^6.4.0",
57 | "lint-staged": "^13.1.0",
58 | "prettier": "^2.8.2",
59 | "ts-jest": "^29.0.3",
60 | "typescript": "^4.9.4"
61 | },
62 | "prettier": {
63 | "singleQuote": false,
64 | "printWidth": 120,
65 | "tabWidth": 4,
66 | "trailingComma": "none"
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/packages/benchmark/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Change Log
2 |
3 | All notable changes to this project will be documented in this file.
4 | See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5 |
6 |
7 | ## [1.0.4](https://github.com/azu/immutable-array-prototype/compare/v1.0.3...v1.0.4) (2018-03-22)
8 |
9 |
10 |
11 |
12 | **Note:** Version bump only for package benchmark
13 |
14 |
15 | ## [1.0.3](https://github.com/azu/immutable-array-prototype/compare/v1.0.2...v1.0.3) (2017-10-09)
16 |
17 |
18 |
19 |
20 | **Note:** Version bump only for package benchmark
21 |
22 |
23 | ## 1.0.2 (2017-06-24)
24 |
25 |
26 |
27 |
28 | ## 1.0.1 (2017-06-24)
29 |
30 |
31 | ### Performance Improvements
32 |
33 | * **benchmark:** add Benchmark ([#23](https://github.com/azu/immutable-array-prototype/issues/23)) ([c9ee918](https://github.com/azu/immutable-array-prototype/commit/c9ee918))
34 |
35 |
36 |
37 |
38 |
39 | ## 1.0.1 (2017-06-24)
40 |
41 |
42 | ### Performance Improvements
43 |
44 | * **benchmark:** add Benchmark ([#23](https://github.com/azu/immutable-array-prototype/issues/23)) ([c9ee918](https://github.com/azu/immutable-array-prototype/commit/c9ee918))
45 |
--------------------------------------------------------------------------------
/packages/benchmark/README.md:
--------------------------------------------------------------------------------
1 | # Benchmark
2 |
3 | Benchmark native array prototype methods vs. `@immutable-array`
4 |
5 | ## Usage
6 |
7 | npm run compare
8 |
9 | ## Example Output
10 |
11 | > nanobench-compare output/array.log output/immutable-array.log
12 |
13 | NANOBENCH version 2 | NANOBENCH version 2
14 | > node src/array.js | > immutable-array.js
15 | |
16 | # pop 200000 times <<< # pop 200000 times
17 | ok ~219 ms (0 s + 219071853 ns) <<< ok ~296 ms (0 s + 295642472 ns)
18 | |
19 | # push 200000 times <<< # push 200000 times
20 | ok ~37 ms (0 s + 36657200 ns) <<< ok ~136 ms (0 s + 136168374 ns)
21 | |
22 | # shift 200000 times <<< # shift 200000 times
23 | ok ~218 ms (0 s + 218119977 ns) <<< ok ~267 ms (0 s + 266877015 ns)
24 | |
25 | # unshift 200000 times <<< # unshift 200000 times
26 | ok ~43 ms (0 s + 42725823 ns) <<< ok ~96 ms (0 s + 96198980 ns)
27 | |
28 | # sort 2000 times === # sort 2000 times
29 | ok ~554 ms (0 s + 554139393 ns) === ok ~566 ms (0 s + 566213053 ns)
30 | |
31 | # reverse 200000 times >>> # reverse 200000 times
32 | ok ~502 ms (0 s + 501891778 ns) >>> ok ~426 ms (0 s + 425654285 ns)
33 | |
34 | # fill 200000 times <<< # fill 200000 times
35 | ok ~693 ms (0 s + 692615788 ns) <<< ok ~1.26 s (1 s + 259820993 ns)
36 | |
37 | # splice 200000 times <<< # splice 200000 times
38 | ok ~250 ms (0 s + 249596808 ns) <<< ok ~609 ms (0 s + 608638182 ns)
39 | |
40 | # copyWithin 200000 times <<< # copyWithin 200000 times
41 | ok ~186 ms (0 s + 186430568 ns) <<< ok ~279 ms (0 s + 279108176 ns)
42 | |
43 | all benchmarks completed <<< all benchmarks completed
44 | ok ~2.7 s (2 s + 701249188 ns) <<< ok ~3.93 s (3 s + 934321530 ns)
45 | |
46 |
--------------------------------------------------------------------------------
/packages/benchmark/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "private": true,
3 | "name": "benchmark",
4 | "version": "1.0.6",
5 | "description": "benchmark for @immutable-array",
6 | "main": "benchamrk.js",
7 | "scripts": {
8 | "array": "node src/array.js > output/array.log",
9 | "immutable-array": "node src/immutable-array.js > output/immutable-array.log",
10 | "precompare": "mkdir -p output && npm run array && npm run immutable-array",
11 | "compare": "nanobench-compare output/array.log output/immutable-array.log",
12 | "test": "jest"
13 | },
14 | "keywords": [
15 | "benchmark"
16 | ],
17 | "author": "azu",
18 | "license": "MIT",
19 | "devDependencies": {
20 | "@immutable-array/prototype": "^1.0.6",
21 | "@types/jest": "^29.2.5",
22 | "jest": "^29.3.1",
23 | "nanobench": "^3.0.0",
24 | "ts-jest": "^29.0.3"
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/packages/benchmark/src/array.js:
--------------------------------------------------------------------------------
1 | // MIT © 2017 azu
2 | "use strict";
3 | const bench = require("nanobench");
4 | const { noop, createSmallArray, createBigArray } = require("./util");
5 | const executionCount = 200000;
6 | bench(`pop ${executionCount} times`, function(b) {
7 | b.start();
8 | for (var i = 0; i < executionCount; i++) {
9 | var smallArray = createSmallArray();
10 | var bigArray = createBigArray();
11 | noop(smallArray.pop());
12 | noop(bigArray.pop());
13 | noop(smallArray);
14 | noop(bigArray);
15 | }
16 | b.end();
17 | });
18 | bench(`push ${executionCount} times`, function(b) {
19 | b.start();
20 | for (var i = 0; i < executionCount; i++) {
21 | var smallArray = createSmallArray();
22 | var bigArray = createBigArray();
23 | noop(smallArray.push(i));
24 | noop(smallArray.push(i));
25 | noop(smallArray);
26 | noop(bigArray);
27 | }
28 | b.end();
29 | });
30 | bench(`shift ${executionCount} times`, function(b) {
31 | b.start();
32 | for (var i = 0; i < executionCount; i++) {
33 | var smallArray = createSmallArray();
34 | var bigArray = createBigArray();
35 | noop(smallArray.shift());
36 | noop(bigArray.shift());
37 | noop(smallArray);
38 | noop(bigArray);
39 | }
40 | b.end();
41 | });
42 | bench(`unshift ${executionCount} times`, function(b) {
43 | b.start();
44 | for (var i = 0; i < executionCount; i++) {
45 | var smallArray = createSmallArray();
46 | var bigArray = createBigArray();
47 | noop(smallArray.unshift(i));
48 | noop(smallArray.unshift(i));
49 | noop(smallArray);
50 | noop(bigArray);
51 | }
52 | b.end();
53 | });
54 | bench(`sort ${executionCount / 100} times`, function(b) {
55 | b.start();
56 | for (var i = 0; i < executionCount / 100; i++) {
57 | var smallArray = createSmallArray();
58 | var bigArray = createBigArray();
59 | noop(smallArray.sort());
60 | noop(bigArray.sort());
61 | noop(smallArray);
62 | noop(bigArray);
63 | }
64 | b.end();
65 | });
66 |
67 | bench(`reverse ${executionCount} times`, function(b) {
68 | b.start();
69 | for (var i = 0; i < executionCount; i++) {
70 | var smallArray = createSmallArray();
71 | var bigArray = createBigArray();
72 | noop(smallArray.reverse());
73 | noop(bigArray.reverse());
74 | noop(smallArray);
75 | noop(bigArray);
76 | }
77 | b.end();
78 | });
79 | bench(`fill ${executionCount} times`, function(b) {
80 | b.start();
81 | for (var i = 0; i < executionCount; i++) {
82 | var smallArray = createSmallArray();
83 | var bigArray = createBigArray();
84 | noop(smallArray.fill(1));
85 | noop(bigArray.fill(1));
86 | noop(smallArray);
87 | noop(bigArray);
88 | }
89 | b.end();
90 | });
91 | bench(`splice ${executionCount} times`, function(b) {
92 | b.start();
93 | for (var i = 0; i < executionCount; i++) {
94 | var smallArray = createSmallArray();
95 | var bigArray = createBigArray();
96 | noop(smallArray.splice(1, 1, "x"));
97 | noop(bigArray.splice(1, 1, "x"));
98 | noop(smallArray);
99 | noop(bigArray);
100 | }
101 | b.end();
102 | });
103 |
104 | bench(`copyWithin ${executionCount} times`, function(b) {
105 | b.start();
106 | for (var i = 0; i < executionCount; i++) {
107 | var smallArray = createSmallArray();
108 | var bigArray = createBigArray();
109 | noop(smallArray.copyWithin(0, 3, 4));
110 | noop(bigArray.copyWithin(0, 3, 4));
111 | noop(smallArray);
112 | noop(bigArray);
113 | }
114 | b.end();
115 | });
116 |
--------------------------------------------------------------------------------
/packages/benchmark/src/immutable-array.js:
--------------------------------------------------------------------------------
1 | // MIT © 2017 azu
2 | "use strict";
3 | const bench = require("nanobench");
4 | const { pop, push, shift, unshift, sort, reverse, fill, splice, copyWithin } = require("@immutable-array/prototype");
5 | const { noop, createSmallArray, createBigArray } = require("./util");
6 | const executionCount = 200000;
7 | bench(`pop ${executionCount} times`, function(b) {
8 | b.start();
9 | for (var i = 0; i < executionCount; i++) {
10 | var smallArray = createSmallArray();
11 | var bigArray = createBigArray();
12 | noop(pop(smallArray));
13 | noop(pop(bigArray));
14 | noop(smallArray);
15 | noop(bigArray);
16 | }
17 | b.end();
18 | });
19 | bench(`push ${executionCount} times`, function(b) {
20 | b.start();
21 | for (var i = 0; i < executionCount; i++) {
22 | var smallArray = createSmallArray();
23 | var bigArray = createBigArray();
24 | noop(push(smallArray, i));
25 | noop(push(push(smallArray, i)));
26 | noop(smallArray);
27 | noop(bigArray);
28 | }
29 | b.end();
30 | });
31 | bench(`shift ${executionCount} times`, function(b) {
32 | b.start();
33 | for (var i = 0; i < executionCount; i++) {
34 | var smallArray = createSmallArray();
35 | var bigArray = createBigArray();
36 | noop(shift(smallArray));
37 | noop(shift(bigArray));
38 | noop(smallArray);
39 | noop(bigArray);
40 | }
41 | b.end();
42 | });
43 | bench(`unshift ${executionCount} times`, function(b) {
44 | b.start();
45 | for (var i = 0; i < executionCount; i++) {
46 | var smallArray = createSmallArray();
47 | var bigArray = createBigArray();
48 | noop(unshift(smallArray, i));
49 | noop(unshift(smallArray, i));
50 | noop(smallArray);
51 | noop(bigArray);
52 | }
53 | b.end();
54 | });
55 | bench(`sort ${executionCount / 100} times`, function(b) {
56 | b.start();
57 | for (var i = 0; i < executionCount / 100; i++) {
58 | var smallArray = createSmallArray();
59 | var bigArray = createBigArray();
60 | noop(sort(smallArray));
61 | noop(sort(bigArray));
62 | noop(smallArray);
63 | noop(bigArray);
64 | }
65 | b.end();
66 | });
67 | bench(`reverse ${executionCount} times`, function(b) {
68 | b.start();
69 | for (var i = 0; i < executionCount; i++) {
70 | var smallArray = createSmallArray();
71 | var bigArray = createBigArray();
72 | noop(reverse(smallArray));
73 | noop(reverse(bigArray));
74 | noop(smallArray);
75 | noop(bigArray);
76 | }
77 | b.end();
78 | });
79 | bench(`fill ${executionCount} times`, function(b) {
80 | b.start();
81 | for (var i = 0; i < executionCount; i++) {
82 | var smallArray = createSmallArray();
83 | var bigArray = createBigArray();
84 | noop(fill(smallArray, 1));
85 | noop(fill(bigArray, 1));
86 | noop(smallArray);
87 | noop(bigArray);
88 | }
89 | b.end();
90 | });
91 | bench(`splice ${executionCount} times`, function(b) {
92 | b.start();
93 | for (var i = 0; i < executionCount; i++) {
94 | var smallArray = createSmallArray();
95 | var bigArray = createBigArray();
96 | noop(splice(smallArray, 1, 1, "x"));
97 | noop(splice(bigArray, 1, 1, "x"));
98 | noop(smallArray);
99 | noop(bigArray);
100 | }
101 | b.end();
102 | });
103 |
104 | bench(`copyWithin ${executionCount} times`, function(b) {
105 | b.start();
106 | for (var i = 0; i < executionCount; i++) {
107 | var smallArray = createSmallArray();
108 | var bigArray = createBigArray();
109 | noop(copyWithin(smallArray, 0, 3, 4));
110 | noop(copyWithin(bigArray, 0, 3, 4));
111 | noop(smallArray);
112 | noop(bigArray);
113 | }
114 | b.end();
115 | });
116 |
--------------------------------------------------------------------------------
/packages/benchmark/src/util.js:
--------------------------------------------------------------------------------
1 | // MIT © 2017 azu
2 | "use strict";
3 | /**
4 | *
5 | */
6 | module.exports.noop = () => {
7 | // this is noop for benchmarking
8 | };
9 | /**
10 | * @returns {Array}
11 | */
12 | module.exports.createSmallArray = () => {
13 | return [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
14 | };
15 | /**
16 | * @returns {Array}
17 | */
18 | module.exports.createBigArray = () => {
19 | return [
20 | 0,
21 | 1,
22 | 2,
23 | 3,
24 | 4,
25 | 5,
26 | 6,
27 | 7,
28 | 8,
29 | 9,
30 | 10,
31 | 11,
32 | 12,
33 | 13,
34 | 14,
35 | 15,
36 | 16,
37 | 17,
38 | 18,
39 | 19,
40 | 20,
41 | 21,
42 | 22,
43 | 23,
44 | 24,
45 | 25,
46 | 26,
47 | 27,
48 | 28,
49 | 29,
50 | 30,
51 | 31,
52 | 32,
53 | 33,
54 | 34,
55 | 35,
56 | 36,
57 | 37,
58 | 38,
59 | 39,
60 | 40,
61 | 41,
62 | 42,
63 | 43,
64 | 44,
65 | 45,
66 | 46,
67 | 47,
68 | 48,
69 | 49,
70 | 50,
71 | 51,
72 | 52,
73 | 53,
74 | 54,
75 | 55,
76 | 56,
77 | 57,
78 | 58,
79 | 59,
80 | 60,
81 | 61,
82 | 62,
83 | 63,
84 | 64,
85 | 65,
86 | 66,
87 | 67,
88 | 68,
89 | 69,
90 | 70,
91 | 71,
92 | 72,
93 | 73,
94 | 74,
95 | 75,
96 | 76,
97 | 77,
98 | 78,
99 | 79,
100 | 80,
101 | 81,
102 | 82,
103 | 83,
104 | 84,
105 | 85,
106 | 86,
107 | 87,
108 | 88,
109 | 89,
110 | 90,
111 | 91,
112 | 92,
113 | 93,
114 | 94,
115 | 95,
116 | 96,
117 | 97,
118 | 98,
119 | 99,
120 | 100,
121 | 101,
122 | 102,
123 | 103,
124 | 104,
125 | 105,
126 | 106,
127 | 107,
128 | 108,
129 | 109,
130 | 110,
131 | 111,
132 | 112,
133 | 113,
134 | 114,
135 | 115,
136 | 116,
137 | 117,
138 | 118,
139 | 119,
140 | 120,
141 | 121,
142 | 122,
143 | 123,
144 | 124,
145 | 125,
146 | 126,
147 | 127,
148 | 128,
149 | 129,
150 | 130,
151 | 131,
152 | 132,
153 | 133,
154 | 134,
155 | 135,
156 | 136,
157 | 137,
158 | 138,
159 | 139,
160 | 140,
161 | 141,
162 | 142,
163 | 143,
164 | 144,
165 | 145,
166 | 146,
167 | 147,
168 | 148,
169 | 149,
170 | 150,
171 | 151,
172 | 152,
173 | 153,
174 | 154,
175 | 155,
176 | 156,
177 | 157,
178 | 158,
179 | 159,
180 | 160,
181 | 161,
182 | 162,
183 | 163,
184 | 164,
185 | 165,
186 | 166,
187 | 167,
188 | 168,
189 | 169,
190 | 170,
191 | 171,
192 | 172,
193 | 173,
194 | 174,
195 | 175,
196 | 176,
197 | 177,
198 | 178,
199 | 179,
200 | 180,
201 | 181,
202 | 182,
203 | 183,
204 | 184,
205 | 185,
206 | 186,
207 | 187,
208 | 188,
209 | 189,
210 | 190,
211 | 191,
212 | 192,
213 | 193,
214 | 194,
215 | 195,
216 | 196,
217 | 197,
218 | 198,
219 | 199,
220 | 200,
221 | 201,
222 | 202,
223 | 203,
224 | 204,
225 | 205,
226 | 206,
227 | 207,
228 | 208,
229 | 209,
230 | 210,
231 | 211,
232 | 212,
233 | 213,
234 | 214,
235 | 215,
236 | 216,
237 | 217,
238 | 218,
239 | 219,
240 | 220,
241 | 221,
242 | 222,
243 | 223,
244 | 224,
245 | 225,
246 | 226,
247 | 227,
248 | 228,
249 | 229,
250 | 230,
251 | 231,
252 | 232,
253 | 233,
254 | 234,
255 | 235,
256 | 236,
257 | 237,
258 | 238,
259 | 239,
260 | 240,
261 | 241,
262 | 242,
263 | 243,
264 | 244,
265 | 245,
266 | 246,
267 | 247,
268 | 248,
269 | 249,
270 | 250,
271 | 251,
272 | 252,
273 | 253,
274 | 254,
275 | 255,
276 | 256,
277 | 257,
278 | 258,
279 | 259,
280 | 260,
281 | 261,
282 | 262,
283 | 263,
284 | 264,
285 | 265,
286 | 266,
287 | 267,
288 | 268,
289 | 269,
290 | 270,
291 | 271,
292 | 272,
293 | 273,
294 | 274,
295 | 275,
296 | 276,
297 | 277,
298 | 278,
299 | 279,
300 | 280,
301 | 281,
302 | 282,
303 | 283,
304 | 284,
305 | 285,
306 | 286,
307 | 287,
308 | 288,
309 | 289,
310 | 290,
311 | 291,
312 | 292,
313 | 293,
314 | 294,
315 | 295,
316 | 296,
317 | 297,
318 | 298,
319 | 299,
320 | 300,
321 | 301,
322 | 302,
323 | 303,
324 | 304,
325 | 305,
326 | 306,
327 | 307,
328 | 308,
329 | 309,
330 | 310,
331 | 311,
332 | 312,
333 | 313,
334 | 314,
335 | 315,
336 | 316,
337 | 317,
338 | 318,
339 | 319,
340 | 320,
341 | 321,
342 | 322,
343 | 323,
344 | 324,
345 | 325,
346 | 326,
347 | 327,
348 | 328,
349 | 329,
350 | 330,
351 | 331,
352 | 332,
353 | 333,
354 | 334,
355 | 335,
356 | 336,
357 | 337,
358 | 338,
359 | 339,
360 | 340,
361 | 341,
362 | 342,
363 | 343,
364 | 344,
365 | 345,
366 | 346,
367 | 347,
368 | 348,
369 | 349,
370 | 350,
371 | 351,
372 | 352,
373 | 353,
374 | 354,
375 | 355,
376 | 356,
377 | 357,
378 | 358,
379 | 359,
380 | 360,
381 | 361,
382 | 362,
383 | 363,
384 | 364,
385 | 365,
386 | 366,
387 | 367,
388 | 368,
389 | 369,
390 | 370,
391 | 371,
392 | 372,
393 | 373,
394 | 374,
395 | 375,
396 | 376,
397 | 377,
398 | 378,
399 | 379,
400 | 380,
401 | 381,
402 | 382,
403 | 383,
404 | 384,
405 | 385,
406 | 386,
407 | 387,
408 | 388,
409 | 389,
410 | 390,
411 | 391,
412 | 392,
413 | 393,
414 | 394,
415 | 395,
416 | 396,
417 | 397,
418 | 398,
419 | 399,
420 | 400,
421 | 401,
422 | 402,
423 | 403,
424 | 404,
425 | 405,
426 | 406,
427 | 407,
428 | 408,
429 | 409,
430 | 410,
431 | 411,
432 | 412,
433 | 413,
434 | 414,
435 | 415,
436 | 416,
437 | 417,
438 | 418,
439 | 419,
440 | 420,
441 | 421,
442 | 422,
443 | 423,
444 | 424,
445 | 425,
446 | 426,
447 | 427,
448 | 428,
449 | 429,
450 | 430,
451 | 431,
452 | 432,
453 | 433,
454 | 434,
455 | 435,
456 | 436,
457 | 437,
458 | 438,
459 | 439,
460 | 440,
461 | 441,
462 | 442,
463 | 443,
464 | 444,
465 | 445,
466 | 446,
467 | 447,
468 | 448,
469 | 449,
470 | 450,
471 | 451,
472 | 452,
473 | 453,
474 | 454,
475 | 455,
476 | 456,
477 | 457,
478 | 458,
479 | 459,
480 | 460,
481 | 461,
482 | 462,
483 | 463,
484 | 464,
485 | 465,
486 | 466,
487 | 467,
488 | 468,
489 | 469,
490 | 470,
491 | 471,
492 | 472,
493 | 473,
494 | 474,
495 | 475,
496 | 476,
497 | 477,
498 | 478,
499 | 479,
500 | 480,
501 | 481,
502 | 482,
503 | 483,
504 | 484,
505 | 485,
506 | 486,
507 | 487,
508 | 488,
509 | 489,
510 | 490,
511 | 491,
512 | 492,
513 | 493,
514 | 494,
515 | 495,
516 | 496,
517 | 497,
518 | 498,
519 | 499,
520 | 500,
521 | 501,
522 | 502,
523 | 503,
524 | 504,
525 | 505,
526 | 506,
527 | 507,
528 | 508,
529 | 509,
530 | 510,
531 | 511,
532 | 512,
533 | 513,
534 | 514,
535 | 515,
536 | 516,
537 | 517,
538 | 518,
539 | 519,
540 | 520,
541 | 521,
542 | 522,
543 | 523,
544 | 524,
545 | 525,
546 | 526,
547 | 527,
548 | 528,
549 | 529,
550 | 530,
551 | 531,
552 | 532,
553 | 533,
554 | 534,
555 | 535,
556 | 536,
557 | 537,
558 | 538,
559 | 539,
560 | 540,
561 | 541,
562 | 542,
563 | 543,
564 | 544,
565 | 545,
566 | 546,
567 | 547,
568 | 548,
569 | 549,
570 | 550,
571 | 551,
572 | 552,
573 | 553,
574 | 554,
575 | 555,
576 | 556,
577 | 557,
578 | 558,
579 | 559,
580 | 560,
581 | 561,
582 | 562,
583 | 563,
584 | 564,
585 | 565,
586 | 566,
587 | 567,
588 | 568,
589 | 569,
590 | 570,
591 | 571,
592 | 572,
593 | 573,
594 | 574,
595 | 575,
596 | 576,
597 | 577,
598 | 578,
599 | 579,
600 | 580,
601 | 581,
602 | 582,
603 | 583,
604 | 584,
605 | 585,
606 | 586,
607 | 587,
608 | 588,
609 | 589,
610 | 590,
611 | 591,
612 | 592,
613 | 593,
614 | 594,
615 | 595,
616 | 596,
617 | 597,
618 | 598,
619 | 599,
620 | 600,
621 | 601,
622 | 602,
623 | 603,
624 | 604,
625 | 605,
626 | 606,
627 | 607,
628 | 608,
629 | 609,
630 | 610,
631 | 611,
632 | 612,
633 | 613,
634 | 614,
635 | 615,
636 | 616,
637 | 617,
638 | 618,
639 | 619,
640 | 620,
641 | 621,
642 | 622,
643 | 623,
644 | 624,
645 | 625,
646 | 626,
647 | 627,
648 | 628,
649 | 629,
650 | 630,
651 | 631,
652 | 632,
653 | 633,
654 | 634,
655 | 635,
656 | 636,
657 | 637,
658 | 638,
659 | 639,
660 | 640,
661 | 641,
662 | 642,
663 | 643,
664 | 644,
665 | 645,
666 | 646,
667 | 647,
668 | 648,
669 | 649,
670 | 650,
671 | 651,
672 | 652,
673 | 653,
674 | 654,
675 | 655,
676 | 656,
677 | 657,
678 | 658,
679 | 659,
680 | 660,
681 | 661,
682 | 662,
683 | 663,
684 | 664,
685 | 665,
686 | 666,
687 | 667,
688 | 668,
689 | 669,
690 | 670,
691 | 671,
692 | 672,
693 | 673,
694 | 674,
695 | 675,
696 | 676,
697 | 677,
698 | 678,
699 | 679,
700 | 680,
701 | 681,
702 | 682,
703 | 683,
704 | 684,
705 | 685,
706 | 686,
707 | 687,
708 | 688,
709 | 689,
710 | 690,
711 | 691,
712 | 692,
713 | 693,
714 | 694,
715 | 695,
716 | 696,
717 | 697,
718 | 698,
719 | 699,
720 | 700,
721 | 701,
722 | 702,
723 | 703,
724 | 704,
725 | 705,
726 | 706,
727 | 707,
728 | 708,
729 | 709,
730 | 710,
731 | 711,
732 | 712,
733 | 713,
734 | 714,
735 | 715,
736 | 716,
737 | 717,
738 | 718,
739 | 719,
740 | 720,
741 | 721,
742 | 722,
743 | 723,
744 | 724,
745 | 725,
746 | 726,
747 | 727,
748 | 728,
749 | 729,
750 | 730,
751 | 731,
752 | 732,
753 | 733,
754 | 734,
755 | 735,
756 | 736,
757 | 737,
758 | 738,
759 | 739,
760 | 740,
761 | 741,
762 | 742,
763 | 743,
764 | 744,
765 | 745,
766 | 746,
767 | 747,
768 | 748,
769 | 749,
770 | 750,
771 | 751,
772 | 752,
773 | 753,
774 | 754,
775 | 755,
776 | 756,
777 | 757,
778 | 758,
779 | 759,
780 | 760,
781 | 761,
782 | 762,
783 | 763,
784 | 764,
785 | 765,
786 | 766,
787 | 767,
788 | 768,
789 | 769,
790 | 770,
791 | 771,
792 | 772,
793 | 773,
794 | 774,
795 | 775,
796 | 776,
797 | 777,
798 | 778,
799 | 779,
800 | 780,
801 | 781,
802 | 782,
803 | 783,
804 | 784,
805 | 785,
806 | 786,
807 | 787,
808 | 788,
809 | 789,
810 | 790,
811 | 791,
812 | 792,
813 | 793,
814 | 794,
815 | 795,
816 | 796,
817 | 797,
818 | 798,
819 | 799,
820 | 800,
821 | 801,
822 | 802,
823 | 803,
824 | 804,
825 | 805,
826 | 806,
827 | 807,
828 | 808,
829 | 809,
830 | 810,
831 | 811,
832 | 812,
833 | 813,
834 | 814,
835 | 815,
836 | 816,
837 | 817,
838 | 818,
839 | 819,
840 | 820,
841 | 821,
842 | 822,
843 | 823,
844 | 824,
845 | 825,
846 | 826,
847 | 827,
848 | 828,
849 | 829,
850 | 830,
851 | 831,
852 | 832,
853 | 833,
854 | 834,
855 | 835,
856 | 836,
857 | 837,
858 | 838,
859 | 839,
860 | 840,
861 | 841,
862 | 842,
863 | 843,
864 | 844,
865 | 845,
866 | 846,
867 | 847,
868 | 848,
869 | 849,
870 | 850,
871 | 851,
872 | 852,
873 | 853,
874 | 854,
875 | 855,
876 | 856,
877 | 857,
878 | 858,
879 | 859,
880 | 860,
881 | 861,
882 | 862,
883 | 863,
884 | 864,
885 | 865,
886 | 866,
887 | 867,
888 | 868,
889 | 869,
890 | 870,
891 | 871,
892 | 872,
893 | 873,
894 | 874,
895 | 875,
896 | 876,
897 | 877,
898 | 878,
899 | 879,
900 | 880,
901 | 881,
902 | 882,
903 | 883,
904 | 884,
905 | 885,
906 | 886,
907 | 887,
908 | 888,
909 | 889,
910 | 890,
911 | 891,
912 | 892,
913 | 893,
914 | 894,
915 | 895,
916 | 896,
917 | 897,
918 | 898,
919 | 899,
920 | 900,
921 | 901,
922 | 902,
923 | 903,
924 | 904,
925 | 905,
926 | 906,
927 | 907,
928 | 908,
929 | 909,
930 | 910,
931 | 911,
932 | 912,
933 | 913,
934 | 914,
935 | 915,
936 | 916,
937 | 917,
938 | 918,
939 | 919,
940 | 920,
941 | 921,
942 | 922,
943 | 923,
944 | 924,
945 | 925,
946 | 926,
947 | 927,
948 | 928,
949 | 929,
950 | 930,
951 | 931,
952 | 932,
953 | 933,
954 | 934,
955 | 935,
956 | 936,
957 | 937,
958 | 938,
959 | 939,
960 | 940,
961 | 941,
962 | 942,
963 | 943,
964 | 944,
965 | 945,
966 | 946,
967 | 947,
968 | 948,
969 | 949,
970 | 950,
971 | 951,
972 | 952,
973 | 953,
974 | 954,
975 | 955,
976 | 956,
977 | 957,
978 | 958,
979 | 959,
980 | 960,
981 | 961,
982 | 962,
983 | 963,
984 | 964,
985 | 965,
986 | 966,
987 | 967,
988 | 968,
989 | 969,
990 | 970,
991 | 971,
992 | 972,
993 | 973,
994 | 974,
995 | 975,
996 | 976,
997 | 977,
998 | 978,
999 | 979,
1000 | 980,
1001 | 981,
1002 | 982,
1003 | 983,
1004 | 984,
1005 | 985,
1006 | 986,
1007 | 987,
1008 | 988,
1009 | 989,
1010 | 990,
1011 | 991,
1012 | 992,
1013 | 993,
1014 | 994,
1015 | 995,
1016 | 996,
1017 | 997,
1018 | 998,
1019 | 999
1020 | ];
1021 | };
1022 |
--------------------------------------------------------------------------------
/packages/copy-within/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Change Log
2 |
3 | All notable changes to this project will be documented in this file.
4 | See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5 |
6 |
7 | ## [1.0.4](https://github.com/azu/immutable-array-prototype/compare/v1.0.3...v1.0.4) (2018-03-22)
8 |
9 |
10 |
11 |
12 | **Note:** Version bump only for package @immutable-array/copy-within
13 |
14 |
15 | ## [1.0.3](https://github.com/azu/immutable-array-prototype/compare/v1.0.2...v1.0.3) (2017-10-09)
16 |
17 |
18 |
19 |
20 | **Note:** Version bump only for package @immutable-array/copy-within
21 |
22 |
23 | ## 1.0.1 (2017-06-24)
24 |
25 |
26 | ### Features
27 |
28 | * **copyWithin:** implement immutable Array#copyWithin ([#21](https://github.com/azu/immutable-array-prototype/issues/21)) ([85597d1](https://github.com/azu/immutable-array-prototype/commit/85597d1))
29 | * **prototype:** add [@immutable](https://github.com/immutable)-array/prototype module ([#22](https://github.com/azu/immutable-array-prototype/issues/22)) ([86f9452](https://github.com/azu/immutable-array-prototype/commit/86f9452))
30 |
--------------------------------------------------------------------------------
/packages/copy-within/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2017 azu
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy
4 | of this software and associated documentation files (the "Software"), to deal
5 | in the Software without restriction, including without limitation the rights
6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | copies of the Software, and to permit persons to whom the Software is
8 | furnished to do so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in all
11 | copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 | SOFTWARE.
20 |
--------------------------------------------------------------------------------
/packages/copy-within/README.md:
--------------------------------------------------------------------------------
1 | # @immutable-array/copy-within
2 |
3 | Immutable [`Array.prototype.copyWithin()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/copyWithin).
4 |
5 | ## Install
6 |
7 | Install with [npm](https://www.npmjs.com/):
8 |
9 | npm install @immutable-array/copy-within
10 |
11 | ## Usage
12 |
13 | Same API with [`Array.prototype.copyWithin()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/copyWithin)
14 |
15 | ```ts
16 | /**
17 | * Returns the this object after copying a section of the array identified by start and end
18 | * to the same array starting at position target
19 | * @param array Base array
20 | * @param target If target is negative, it is treated as length+target where length is the
21 | * length of the array.
22 | * @param start If start is negative, it is treated as length+start. If end is negative, it
23 | * is treated as length+end.
24 | * @param end If not specified, length of the this object is used as its default value.
25 | */
26 | export declare function copyWithin(array: Array, target: number, start: number, end?: number): Array;
27 | ```
28 |
29 | ## Example
30 |
31 | ```js
32 | import { copyWithin } from "@immutable-array/copyWithin";
33 | const originalArray = [1, 2, 3, 4, 5];
34 | assert.deepStrictEqual(copyWithin(originalArray, -2), [1, 2, 3, 1, 2]);
35 | assert.deepStrictEqual(copyWithin(originalArray, 0, 3), [4, 5, 3, 4, 5]);
36 | assert.deepStrictEqual(copyWithin(originalArray, 0, 3, 4), [4, 2, 3, 4, 5]);
37 | assert.deepStrictEqual(copyWithin(originalArray, 0, -2, -1), [4, 2, 3, 4, 5]);
38 | ```
39 |
40 | TODO: Array-like.
41 |
42 | This is not specified behavior.
43 |
44 | If you interesting in this, Please comment to [copyWithIn() with Array-like · Issue #20 · azu/immutable-array-prototype](https://github.com/azu/immutable-array-prototype/issues/20 "copyWithIn() with Array-like · Issue #20 · azu/immutable-array-prototype").
45 |
46 | ```js
47 | import { copyWithin } from "@immutable-array/copyWithin";
48 | const arrayLike = { length: 5, 3: 1 };
49 | let actual = copyWithin(arrayLike, 0, 3);
50 | assert.deepStrictEqual(actual, [1, , , 1,]);
51 | ```
52 |
53 | ## Changelog
54 |
55 | See [Releases page](https://github.com/azu/immutable-array-prototype/releases).
56 |
57 | ## Running tests
58 |
59 | Install devDependencies and Run `npm test`:
60 |
61 | npm i -d && npm test
62 |
63 | ## Contributing
64 |
65 | Pull requests and stars are always welcome.
66 |
67 | For bugs and feature requests, [please create an issue](https://github.com/azu/immutable-array-prototype/issues).
68 |
69 | 1. Fork it!
70 | 2. Create your feature branch: `git checkout -b my-new-feature`
71 | 3. Commit your changes: `git commit -am 'Add some feature'`
72 | 4. Push to the branch: `git push origin my-new-feature`
73 | 5. Submit a pull request :D
74 |
75 | ## Author
76 |
77 | - [github/azu](https://github.com/azu)
78 | - [twitter/azu_re](https://twitter.com/azu_re)
79 |
80 | ## License
81 |
82 | MIT © azu
83 |
--------------------------------------------------------------------------------
/packages/copy-within/jest.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | "transform": {
3 | ".(ts|tsx)": "ts-jest"
4 | },
5 | "testRegex": "(./test/.*-(test|spec))\\.(ts|tsx|js)$",
6 | "testPathIgnorePatterns": ["/lib/", "/out/", "/node_modules/"],
7 | "moduleFileExtensions": [
8 | "ts",
9 | "tsx",
10 | "js"
11 | ]
12 | };
13 |
--------------------------------------------------------------------------------
/packages/copy-within/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "directories": {
3 | "lib": "lib",
4 | "test": "test"
5 | },
6 | "author": "azu",
7 | "license": "MIT",
8 | "files": [
9 | "bin/",
10 | "lib/",
11 | "src/"
12 | ],
13 | "name": "@immutable-array/copy-within",
14 | "version": "1.0.6",
15 | "description": "Immutable Array.prototype.copyWithin().",
16 | "main": "lib/copy-within.js",
17 | "types": "lib/copy-within.d.ts",
18 | "scripts": {
19 | "build": "tsc -p .",
20 | "watch": "tsc -p . --watch",
21 | "prepublish": "npm run --if-present build",
22 | "test": "jest"
23 | },
24 | "publishConfig": {
25 | "access": "public"
26 | },
27 | "keywords": [
28 | "array",
29 | "immutable"
30 | ],
31 | "repository": {
32 | "type": "git",
33 | "url": "https://github.com/azu/immutable-array-prototype.git"
34 | },
35 | "bugs": {
36 | "url": "https://github.com/azu/immutable-array-prototype/issues"
37 | },
38 | "homepage": "https://github.com/azu/immutable-array-prototype/tree/master/packages/copy-within/",
39 | "devDependencies": {
40 | "@types/jest": "^29.2.5",
41 | "@types/node": "^18.11.18",
42 | "jest": "^29.3.1",
43 | "ts-jest": "^29.0.3",
44 | "typescript": "^4.9.4"
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/packages/copy-within/src/copy-within.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * Returns the this object after copying a section of the array identified by start and end
3 | * to the same array starting at position target
4 | * @param array Base array
5 | * @param target If target is negative, it is treated as length+target where length is the
6 | * length of the array.
7 | * @param start If start is negative, it is treated as length+start. If end is negative, it
8 | * is treated as length+end.
9 | * @param end If not specified, length of the this object is used as its default value.
10 | */
11 | export function copyWithin(array: Array, target: number, start: number, end?: number): Array {
12 | return Array.prototype.slice.call(array).copyWithin(target, start, end);
13 | }
14 |
--------------------------------------------------------------------------------
/packages/copy-within/test/copy-within-test.ts:
--------------------------------------------------------------------------------
1 | // MIT © 2017 azu
2 | import * as assert from "assert";
3 | import { copyWithin } from "../src/copy-within";
4 |
5 | describe("copyWithin", () => {
6 | it("should return immutable array", () => {
7 | const originalArray = ["a", "b", "c", "d", "e"];
8 | const resultArray = copyWithin(originalArray, 0, originalArray.length - 1);
9 | assert.ok(originalArray !== resultArray);
10 | });
11 |
12 | it("can copyWithin empty array", () => {
13 | const originalArray: any[] = [];
14 | assert.deepStrictEqual(originalArray.copyWithin(0, 0), []);
15 | });
16 |
17 | it("should be idempotent - x() === x()", () => {
18 | const originalArray = ["a", "b", "c", "d", "e"];
19 | assert.deepStrictEqual(copyWithin(originalArray, -2, 1), copyWithin(originalArray, -2, 1));
20 | });
21 |
22 | it("should work copyWithin", () => {
23 | // https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/copyWithin
24 | const originalArray = [1, 2, 3, 4, 5];
25 | assert.deepStrictEqual(copyWithin(originalArray, -2, 0), [1, 2, 3, 1, 2]);
26 | assert.deepStrictEqual(copyWithin(originalArray, 0, 3), [4, 5, 3, 4, 5]);
27 | assert.deepStrictEqual(copyWithin(originalArray, 0, 3, 4), [4, 2, 3, 4, 5]);
28 | assert.deepStrictEqual(copyWithin(originalArray, 0, -2, -1), [4, 2, 3, 4, 5]);
29 | });
30 | it("should copyWithin to Array-like", () => {
31 | const arrayLike = { length: 5, 3: 1 };
32 | let actual = copyWithin(arrayLike as any, 0, 3);
33 | assert.deepStrictEqual(actual, [1, , , 1, ,]);
34 | });
35 | });
36 |
--------------------------------------------------------------------------------
/packages/copy-within/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": "../../tsconfig.json",
3 | "compilerOptions": {
4 | "outDir": "./lib/"
5 | },
6 | "include": [
7 | "src/**/*"
8 | ]
9 | }
10 |
--------------------------------------------------------------------------------
/packages/fill/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Change Log
2 |
3 | All notable changes to this project will be documented in this file.
4 | See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5 |
6 |
7 | ## [1.0.4](https://github.com/azu/immutable-array-prototype/compare/v1.0.3...v1.0.4) (2018-03-22)
8 |
9 |
10 |
11 |
12 | **Note:** Version bump only for package @immutable-array/fill
13 |
14 |
15 | ## 1.0.1 (2017-06-24)
16 |
17 |
18 | ### Features
19 |
20 | * **fill:** implement immutable Array#fill ([#17](https://github.com/azu/immutable-array-prototype/issues/17)) ([6b3b7d5](https://github.com/azu/immutable-array-prototype/commit/6b3b7d5))
21 | * **prototype:** add [@immutable](https://github.com/immutable)-array/prototype module ([#22](https://github.com/azu/immutable-array-prototype/issues/22)) ([86f9452](https://github.com/azu/immutable-array-prototype/commit/86f9452))
22 |
--------------------------------------------------------------------------------
/packages/fill/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2017 azu
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy
4 | of this software and associated documentation files (the "Software"), to deal
5 | in the Software without restriction, including without limitation the rights
6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | copies of the Software, and to permit persons to whom the Software is
8 | furnished to do so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in all
11 | copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 | SOFTWARE.
20 |
--------------------------------------------------------------------------------
/packages/fill/README.md:
--------------------------------------------------------------------------------
1 | # @immutable-array/fill
2 |
3 | Immutable [`Array.prototype.fill()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/fill).
4 |
5 | ## Install
6 |
7 | Install with [npm](https://www.npmjs.com/):
8 |
9 | npm install @immutable-array/fill
10 |
11 | ## Usage
12 |
13 | Same API with [`Array.prototype.fill()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/fill).
14 |
15 | ```ts
16 | /**
17 | * Returns the this object after filling the section identified by start and end with value
18 | * @param array base array
19 | * @param value value to fill array section with
20 | * @param start index to start filling the array at. If start is negative, it is treated as
21 | * length+start where length is the length of the array.
22 | * @param end index to stop filling the array at. If end is negative, it is treated as
23 | * length+end.
24 | */
25 | export declare function fill(array: Array, value: T, start?: number, end?: number): Array;
26 | ```
27 |
28 | ## Example
29 |
30 | ```js
31 | import { fill } from "@immutable-array/fill"
32 | const originalArray = [1, 2, 3];
33 | assert.deepStrictEqual(fill(originalArray, "x"), ["x", "x", "x"]);
34 | assert.deepStrictEqual(fill(originalArray, "x", 1, 2), [1, "x", 3]);
35 | ```
36 |
37 | ## Changelog
38 |
39 | See [Releases page](https://github.com/azu/immutable-array-prototype/releases).
40 |
41 | ## Running tests
42 |
43 | Install devDependencies and Run `npm test`:
44 |
45 | npm i -d && npm test
46 |
47 | ## Contributing
48 |
49 | Pull requests and stars are always welcome.
50 |
51 | For bugs and feature requests, [please create an issue](https://github.com/azu/immutable-array-prototype/issues).
52 |
53 | 1. Fork it!
54 | 2. Create your feature branch: `git checkout -b my-new-feature`
55 | 3. Commit your changes: `git commit -am 'Add some feature'`
56 | 4. Push to the branch: `git push origin my-new-feature`
57 | 5. Submit a pull request :D
58 |
59 | ## Author
60 |
61 | - [github/azu](https://github.com/azu)
62 | - [twitter/azu_re](https://twitter.com/azu_re)
63 |
64 | ## License
65 |
66 | MIT © azu
67 |
--------------------------------------------------------------------------------
/packages/fill/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "directories": {
3 | "lib": "lib",
4 | "test": "test"
5 | },
6 | "author": "azu",
7 | "license": "MIT",
8 | "files": [
9 | "bin/",
10 | "lib/",
11 | "src/"
12 | ],
13 | "name": "@immutable-array/fill",
14 | "version": "1.0.6",
15 | "description": "Immutable Array.prototype.fill.",
16 | "main": "lib/fill.js",
17 | "types": "lib/fill.d.ts",
18 | "scripts": {
19 | "build": "tsc -p .",
20 | "watch": "tsc -p . --watch",
21 | "prepublish": "npm run --if-present build",
22 | "test": "jest"
23 | },
24 | "publishConfig": {
25 | "access": "public"
26 | },
27 | "keywords": [
28 | "array",
29 | "immutable"
30 | ],
31 | "repository": {
32 | "type": "git",
33 | "url": "https://github.com/azu/immutable-array-prototype.git"
34 | },
35 | "bugs": {
36 | "url": "https://github.com/azu/immutable-array-prototype/issues"
37 | },
38 | "homepage": "https://github.com/azu/immutable-array-prototype/tree/master/packages/fill/",
39 | "devDependencies": {
40 | "@types/jest": "^29.2.5",
41 | "@types/node": "^18.11.18",
42 | "jest": "^29.3.1",
43 | "ts-jest": "^29.0.3",
44 | "typescript": "^4.9.4"
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/packages/fill/src/fill.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * Returns the this object after filling the section identified by start and end with value
3 | * @param array base array
4 | * @param value value to fill array section with
5 | * @param start index to start filling the array at. If start is negative, it is treated as
6 | * length+start where length is the length of the array.
7 | * @param end index to stop filling the array at. If end is negative, it is treated as
8 | * length+end.
9 | */
10 | export function fill(array: Array, value: T, start?: number, end?: number): Array {
11 | return Array.prototype.slice.call(array).fill(value, start, end);
12 | }
13 |
--------------------------------------------------------------------------------
/packages/fill/test/fill-test.ts:
--------------------------------------------------------------------------------
1 | // MIT © 2017 azu
2 | import { fill } from "../src/fill";
3 | import * as assert from "assert";
4 |
5 | describe("fill", () => {
6 | it("throw error when pass undefined as array", () => {
7 | assert.throws(() => {
8 | fill(undefined);
9 | }, "array is null or undefined");
10 | });
11 | it("can fill element to empty array", () => {
12 | const originalArray = [];
13 | const resultArray = fill(originalArray, "x");
14 | assert.deepStrictEqual(resultArray, []);
15 | });
16 | it("should return immutable result array", () => {
17 | const originalArray = ["a", "b", "c", "d", "e"];
18 | const resultArray = fill(originalArray, 1);
19 | assert.ok(originalArray !== resultArray);
20 | });
21 | it("should fill all elements with value", () => {
22 | const originalArray = [1, 2, 3];
23 | assert.deepStrictEqual(fill(originalArray, "x"), ["x", "x", "x"]);
24 | });
25 | it("should fill elements, start:1", () => {
26 | const originalArray = [1, 2, 3];
27 | assert.deepStrictEqual(fill(originalArray, "x", 1), [1, "x", "x"]);
28 | });
29 | it("should fill elements, start:-1", () => {
30 | const originalArray = [1, 2, 3];
31 | assert.deepStrictEqual(fill(originalArray, "x", -1), [1, 2, "x"]);
32 | });
33 | it("should fill elements, start:1 end:2", () => {
34 | const originalArray = [1, 2, 3];
35 | assert.deepStrictEqual(fill(originalArray, "x", 1, 2), [1, "x", 3]);
36 | });
37 | it("should fill by negative start, start:-3 end:-2 => start:0 end:1", () => {
38 | const originalArray = [1, 2, 3];
39 | assert.deepStrictEqual(fill(originalArray, "x", -3, -2), ["x", 2, 3]);
40 | });
41 | it("should fill non-dense array", () => {
42 | // non-dense
43 | assert.deepStrictEqual(fill(Array(3), "x"), ["x", "x", "x"]);
44 | });
45 | it("should fill array-like", () => {
46 | assert.deepStrictEqual(fill({ length: 3 }, "x"), ["x", "x", "x"]);
47 | });
48 | it("should not fill when start === end", () => {
49 | const originalArray = [1, 2, 3];
50 | assert.deepStrictEqual(fill(originalArray, "x", 1, 1), [1, 2, 3]);
51 | });
52 | it("should not fill when start and end is NaN", () => {
53 | const originalArray = [1, 2, 3];
54 | assert.deepStrictEqual(fill(originalArray, "x", NaN, NaN), [1, 2, 3]);
55 | });
56 |
57 | it("should not fill when start and end is out of range", () => {
58 | const originalArray = [1, 2, 3];
59 | assert.deepStrictEqual(fill(originalArray, "x", -1, 0), [1, 2, 3]);
60 | });
61 | });
62 |
--------------------------------------------------------------------------------
/packages/fill/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": "../../tsconfig.json",
3 | "compilerOptions": {
4 | "outDir": "./lib/"
5 | },
6 | "include": [
7 | "src/**/*"
8 | ]
9 | }
10 |
--------------------------------------------------------------------------------
/packages/pop/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Change Log
2 |
3 | All notable changes to this project will be documented in this file.
4 | See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5 |
6 |
7 | ## [1.0.4](https://github.com/azu/immutable-array-prototype/compare/v1.0.3...v1.0.4) (2018-03-22)
8 |
9 |
10 |
11 |
12 | **Note:** Version bump only for package @immutable-array/pop
13 |
14 |
15 | ## 1.0.1 (2017-06-24)
16 |
17 |
18 | ### Features
19 |
20 | * **pop:** implement immutable Array#pop ([#11](https://github.com/azu/immutable-array-prototype/issues/11)) ([b6fcef7](https://github.com/azu/immutable-array-prototype/commit/b6fcef7))
21 | * **prototype:** add [@immutable](https://github.com/immutable)-array/prototype module ([#22](https://github.com/azu/immutable-array-prototype/issues/22)) ([86f9452](https://github.com/azu/immutable-array-prototype/commit/86f9452))
22 |
--------------------------------------------------------------------------------
/packages/pop/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2017 azu
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy
4 | of this software and associated documentation files (the "Software"), to deal
5 | in the Software without restriction, including without limitation the rights
6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | copies of the Software, and to permit persons to whom the Software is
8 | furnished to do so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in all
11 | copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 | SOFTWARE.
20 |
--------------------------------------------------------------------------------
/packages/pop/README.md:
--------------------------------------------------------------------------------
1 | # @immutable-array/pop
2 |
3 | Immutable [`Array.prototype.pop()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/pop)
4 |
5 | ## Install
6 |
7 | Install with [npm](https://www.npmjs.com/):
8 |
9 | npm install @immutable-array/pop
10 |
11 | ## Usage
12 |
13 | Same API with [`Array.prototype.pop()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/pop)
14 |
15 | ```ts
16 | /**
17 | * Removes the last element from an array and returns it.
18 | */
19 | export declare function pop(array: Array): T[];
20 | ```
21 |
22 | ## Example
23 |
24 | ```js
25 | import { pop } from "@immutable-array/pop";
26 | const originalArray = ["a", "b", "c", "d", "e"];
27 | const resultArray = pop(originalArray);
28 | assert.deepStrictEqual(resultArray, ["a", "b", "c", "d"]);
29 | ```
30 |
31 | ## Changelog
32 |
33 | See [Releases page](https://github.com/azu/immutable-array-prototype/releases).
34 |
35 | ## Running tests
36 |
37 | Install devDependencies and Run `npm test`:
38 |
39 | npm i -d && npm test
40 |
41 | ## Contributing
42 |
43 | Pull requests and stars are always welcome.
44 |
45 | For bugs and feature requests, [please create an issue](https://github.com/azu/immutable-array-prototype/issues).
46 |
47 | 1. Fork it!
48 | 2. Create your feature branch: `git checkout -b my-new-feature`
49 | 3. Commit your changes: `git commit -am 'Add some feature'`
50 | 4. Push to the branch: `git push origin my-new-feature`
51 | 5. Submit a pull request :D
52 |
53 | ## Author
54 |
55 | - [github/azu](https://github.com/azu)
56 | - [twitter/azu_re](https://twitter.com/azu_re)
57 |
58 | ## License
59 |
60 | MIT © azu
61 |
--------------------------------------------------------------------------------
/packages/pop/jest.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | "transform": {
3 | ".(ts|tsx)": "ts-jest"
4 | },
5 | "testRegex": "(./test/.*-(test|spec))\\.(ts|tsx|js)$",
6 | "testPathIgnorePatterns": ["/lib/", "/out/", "/node_modules/"],
7 | "moduleFileExtensions": [
8 | "ts",
9 | "tsx",
10 | "js"
11 | ]
12 | };
13 |
--------------------------------------------------------------------------------
/packages/pop/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "directories": {
3 | "lib": "lib",
4 | "test": "test"
5 | },
6 | "author": "azu",
7 | "license": "MIT",
8 | "files": [
9 | "bin/",
10 | "lib/",
11 | "src/"
12 | ],
13 | "name": "@immutable-array/pop",
14 | "version": "1.0.6",
15 | "description": "Immutable Array.prototype.pop.",
16 | "main": "lib/pop.js",
17 | "types": "lib/pop.d.ts",
18 | "scripts": {
19 | "build": "tsc -p .",
20 | "watch": "tsc -p . --watch",
21 | "prepublish": "npm run --if-present build",
22 | "test": "jest"
23 | },
24 | "publishConfig": {
25 | "access": "public"
26 | },
27 | "keywords": [
28 | "array",
29 | "immutable"
30 | ],
31 | "repository": {
32 | "type": "git",
33 | "url": "https://github.com/azu/immutable-array-prototype.git"
34 | },
35 | "bugs": {
36 | "url": "https://github.com/azu/immutable-array-prototype/issues"
37 | },
38 | "homepage": "https://github.com/azu/immutable-array-prototype/tree/master/packages/pop/",
39 | "devDependencies": {
40 | "@types/jest": "^29.2.5",
41 | "@types/node": "^18.11.18",
42 | "jest": "^29.3.1",
43 | "ts-jest": "^29.0.3",
44 | "typescript": "^4.9.4"
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/packages/pop/src/pop.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * Removes the last element from an array and returns it.
3 | */
4 | export function pop(array: Array): T[] {
5 | return array.slice(0, -1);
6 | }
7 |
--------------------------------------------------------------------------------
/packages/pop/test/pop-test.ts:
--------------------------------------------------------------------------------
1 | // MIT © 2017 azu
2 | import * as assert from "assert";
3 | import { pop } from "../src/pop";
4 |
5 | describe("pop", () => {
6 | it("should return empty array when pop empty array", () => {
7 | const originalArray: any[] = [];
8 | const resultArray = pop(originalArray);
9 | assert.deepStrictEqual(resultArray, []);
10 | });
11 | it("should return popped array", () => {
12 | const originalArray = ["a", "b", "c", "d", "e"];
13 | const resultArray = pop(originalArray);
14 | assert.deepStrictEqual(resultArray, ["a", "b", "c", "d"]);
15 | });
16 | it("should return immutable result array", () => {
17 | const originalArray = ["a", "b", "c", "d", "e"];
18 | const resultArray = pop(originalArray);
19 | assert.ok(originalArray !== resultArray);
20 | });
21 | it("should be idempotent - x() === x()", () => {
22 | const originalArray = ["a", "b", "c", "d", "e"];
23 | assert.deepStrictEqual(pop(originalArray), pop(originalArray));
24 | });
25 | });
26 |
--------------------------------------------------------------------------------
/packages/pop/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": "../../tsconfig.json",
3 | "compilerOptions": {
4 | "outDir": "./lib/"
5 | },
6 | "include": [
7 | "src/**/*"
8 | ]
9 | }
10 |
--------------------------------------------------------------------------------
/packages/prototype/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Change Log
2 |
3 | All notable changes to this project will be documented in this file.
4 | See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5 |
6 |
7 | ## [1.0.4](https://github.com/azu/immutable-array-prototype/compare/v1.0.3...v1.0.4) (2018-03-22)
8 |
9 |
10 |
11 |
12 | **Note:** Version bump only for package @immutable-array/prototype
13 |
14 |
15 | ## [1.0.3](https://github.com/azu/immutable-array-prototype/compare/v1.0.2...v1.0.3) (2017-10-09)
16 |
17 |
18 |
19 |
20 | **Note:** Version bump only for package @immutable-array/prototype
21 |
22 |
23 | ## 1.0.2 (2017-06-24)
24 |
25 |
26 |
27 |
28 | ## 1.0.1 (2017-06-24)
29 |
30 |
31 | ### Features
32 |
33 | * **prototype:** add [@immutable](https://github.com/immutable)-array/prototype module ([#22](https://github.com/azu/immutable-array-prototype/issues/22)) ([86f9452](https://github.com/azu/immutable-array-prototype/commit/86f9452))
34 |
35 |
36 |
37 |
38 |
39 | ## 1.0.1 (2017-06-24)
40 |
41 |
42 | ### Features
43 |
44 | * **prototype:** add [@immutable](https://github.com/immutable)-array/prototype module ([#22](https://github.com/azu/immutable-array-prototype/issues/22)) ([86f9452](https://github.com/azu/immutable-array-prototype/commit/86f9452))
45 |
--------------------------------------------------------------------------------
/packages/prototype/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2017 azu
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy
4 | of this software and associated documentation files (the "Software"), to deal
5 | in the Software without restriction, including without limitation the rights
6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | copies of the Software, and to permit persons to whom the Software is
8 | furnished to do so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in all
11 | copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 | SOFTWARE.
20 |
--------------------------------------------------------------------------------
/packages/prototype/README.md:
--------------------------------------------------------------------------------
1 | # @immutable-array/prototype
2 |
3 | Collection of immutable `Array.prototype` methods.
4 |
5 | ## Install
6 |
7 | Install with [npm](https://www.npmjs.com/):
8 |
9 | npm install @immutable-array/prototype
10 |
11 | ## Usage
12 |
13 | Please see [README.md on the root](../../README.md).
14 |
15 | ## Changelog
16 |
17 | See [Releases page](https://github.com/azu/immutable-array-prototype/releases).
18 |
19 | ## Running tests
20 |
21 | Install devDependencies and Run `npm test`:
22 |
23 | npm i -d && npm test
24 |
25 | ## Contributing
26 |
27 | Pull requests and stars are always welcome.
28 |
29 | For bugs and feature requests, [please create an issue](https://github.com/azu/immutable-array-prototype/issues).
30 |
31 | 1. Fork it!
32 | 2. Create your feature branch: `git checkout -b my-new-feature`
33 | 3. Commit your changes: `git commit -am 'Add some feature'`
34 | 4. Push to the branch: `git push origin my-new-feature`
35 | 5. Submit a pull request :D
36 |
37 | ## Author
38 |
39 | - [github/azu](https://github.com/azu)
40 | - [twitter/azu_re](https://twitter.com/azu_re)
41 |
42 | ## License
43 |
44 | MIT © azu
45 |
--------------------------------------------------------------------------------
/packages/prototype/jest.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | "transform": {
3 | ".(ts|tsx)": "ts-jest"
4 | },
5 | "testRegex": "(./test/.*-(test|spec))\\.(ts|tsx|js)$",
6 | "testPathIgnorePatterns": ["/lib/", "/out/", "/node_modules/"],
7 | "moduleFileExtensions": [
8 | "ts",
9 | "tsx",
10 | "js"
11 | ]
12 | };
13 |
--------------------------------------------------------------------------------
/packages/prototype/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "directories": {
3 | "lib": "lib",
4 | "test": "test"
5 | },
6 | "author": "azu",
7 | "license": "MIT",
8 | "files": [
9 | "bin/",
10 | "lib/",
11 | "src/"
12 | ],
13 | "name": "@immutable-array/prototype",
14 | "version": "1.0.6",
15 | "description": "Collection of immutable Array.prototype methods.",
16 | "main": "lib/prototype.js",
17 | "types": "lib/prototype.d.ts",
18 | "scripts": {
19 | "build": "tsc -p .",
20 | "watch": "tsc -p . --watch",
21 | "prepublish": "npm run --if-present build",
22 | "test": "jest"
23 | },
24 | "publishConfig": {
25 | "access": "public"
26 | },
27 | "keywords": [
28 | "array",
29 | "immutable",
30 | "collection",
31 | "util",
32 | "structure"
33 | ],
34 | "repository": {
35 | "type": "git",
36 | "url": "https://github.com/azu/immutable-array-prototype.git"
37 | },
38 | "bugs": {
39 | "url": "https://github.com/azu/immutable-array-prototype/issues"
40 | },
41 | "homepage": "https://github.com/azu/immutable-array-prototype/tree/master/packages/prototype/",
42 | "dependencies": {
43 | "@immutable-array/copy-within": "^1.0.6",
44 | "@immutable-array/fill": "^1.0.6",
45 | "@immutable-array/pop": "^1.0.6",
46 | "@immutable-array/push": "^1.0.6",
47 | "@immutable-array/reverse": "^1.0.6",
48 | "@immutable-array/shift": "^1.0.6",
49 | "@immutable-array/sort": "^1.0.6",
50 | "@immutable-array/splice": "^1.0.6",
51 | "@immutable-array/unshift": "^1.0.6"
52 | },
53 | "devDependencies": {
54 | "@types/jest": "^29.2.5",
55 | "@types/node": "^18.11.18",
56 | "jest": "^29.3.1",
57 | "ts-jest": "^29.0.3",
58 | "typescript": "^4.9.4"
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/packages/prototype/src/prototype.ts:
--------------------------------------------------------------------------------
1 | // MIT © 2017 azu
2 | export { pop } from "@immutable-array/pop";
3 | export { push } from "@immutable-array/push";
4 | export { shift } from "@immutable-array/shift";
5 | export { unshift } from "@immutable-array/unshift";
6 | export { sort } from "@immutable-array/sort";
7 | export { reverse } from "@immutable-array/reverse";
8 | export { fill } from "@immutable-array/fill";
9 | export { splice } from "@immutable-array/splice";
10 | export { copyWithin } from "@immutable-array/copy-within";
11 |
--------------------------------------------------------------------------------
/packages/prototype/test/prototype-test.ts:
--------------------------------------------------------------------------------
1 | // MIT © 2017 azu
2 | import * as assert from "assert";
3 | import { sort, unshift, push, fill, splice, pop, reverse, copyWithin, shift } from "../src/prototype";
4 |
5 | describe("prototype", () => {
6 | it("shift", () => {
7 | assert.deepStrictEqual(shift(["a", "b", "c", "d", "e"]), ["b", "c", "d", "e"]);
8 | });
9 | it("unshift", () => {
10 | assert.deepStrictEqual(unshift(["a", "b", "c", "d", "e"], "x"), ["x", "a", "b", "c", "d", "e"]);
11 | });
12 | it("pop", () => {
13 | assert.deepStrictEqual(pop(["a", "b", "c", "d", "e"]), ["a", "b", "c", "d"]);
14 | });
15 | it("push", () => {
16 | assert.deepStrictEqual(push(["a", "b", "c", "d", "e"], "x"), ["a", "b", "c", "d", "e", "x"]);
17 | });
18 | it("splice", () => {
19 | assert.deepStrictEqual(splice(["a", "b", "c", "d", "e"], 0, 1, "x"), ["x", "b", "c", "d", "e"]);
20 | });
21 | it("sort", () => {
22 | assert.deepStrictEqual(sort(["e", "a", "c", "b", "d"]), ["a", "b", "c", "d", "e"]);
23 | });
24 | it("reverse", () => {
25 | assert.deepStrictEqual(reverse(["a", "b", "c", "d", "e"]), ["e", "d", "c", "b", "a"]);
26 | });
27 | it("fill", () => {
28 | assert.deepStrictEqual(fill(new Array(5), "x"), ["x", "x", "x", "x", "x"]);
29 | });
30 | it("copyWithin", () => {
31 | assert.deepStrictEqual(copyWithin(["a", "b", "c", "d", "e"], 0, 3, 4), ["d", "b", "c", "d", "e"]);
32 | });
33 | });
34 |
--------------------------------------------------------------------------------
/packages/prototype/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": "../../tsconfig.json",
3 | "compilerOptions": {
4 | "outDir": "./lib/"
5 | },
6 | "include": [
7 | "src/**/*"
8 | ]
9 | }
--------------------------------------------------------------------------------
/packages/push/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Change Log
2 |
3 | All notable changes to this project will be documented in this file.
4 | See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5 |
6 |
7 | ## [1.0.4](https://github.com/azu/immutable-array-prototype/compare/v1.0.3...v1.0.4) (2018-03-22)
8 |
9 |
10 |
11 |
12 | **Note:** Version bump only for package @immutable-array/push
13 |
14 |
15 | ## 1.0.1 (2017-06-24)
16 |
17 |
18 | ### Bug Fixes
19 |
20 | * **push:** fix the push to allow pushing an array ([#13](https://github.com/azu/immutable-array-prototype/issues/13)) ([24bee3a](https://github.com/azu/immutable-array-prototype/commit/24bee3a))
21 |
22 |
23 | ### Features
24 |
25 | * **prototype:** add [@immutable](https://github.com/immutable)-array/prototype module ([#22](https://github.com/azu/immutable-array-prototype/issues/22)) ([86f9452](https://github.com/azu/immutable-array-prototype/commit/86f9452))
26 | * **push:** implement immutable Array#push ([#1](https://github.com/azu/immutable-array-prototype/issues/1)) ([09b41cc](https://github.com/azu/immutable-array-prototype/commit/09b41cc))
27 |
--------------------------------------------------------------------------------
/packages/push/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2017 azu
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy
4 | of this software and associated documentation files (the "Software"), to deal
5 | in the Software without restriction, including without limitation the rights
6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | copies of the Software, and to permit persons to whom the Software is
8 | furnished to do so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in all
11 | copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 | SOFTWARE.
20 |
--------------------------------------------------------------------------------
/packages/push/README.md:
--------------------------------------------------------------------------------
1 | # @immutable-array/push
2 |
3 | Immutable [`Array.prototype.push()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/push)
4 |
5 | ## Install
6 |
7 | Install with [npm](https://www.npmjs.com/):
8 |
9 | npm install @immutable-array/push
10 |
11 | ## Usage
12 |
13 | Same API with [`Array.prototype.push()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/push).
14 |
15 | ```ts
16 | /**
17 | * Appends new elements to an array, and returns the new length of the array.
18 | * @param array base Array
19 | * @param items New elements of the Array.
20 | */
21 | export declare function push(array: Array, ...items: T[]): Array;
22 | ```
23 |
24 | ## Example
25 |
26 | ```js
27 | import { push } from "@immutable-array/push";
28 | const originalArray = ["a", "b", "c", "d", "e"];
29 | const resultArray = push(originalArray, "f", "g");
30 | assert.ok(originalArray !== resultArray);
31 | ```
32 |
33 | ## Changelog
34 |
35 | See [Releases page](https://github.com/azu/immutable-array-prototype/releases).
36 |
37 | ## Running tests
38 |
39 | Install devDependencies and Run `npm test`:
40 |
41 | npm i -d && npm test
42 |
43 | ## Contributing
44 |
45 | Pull requests and stars are always welcome.
46 |
47 | For bugs and feature requests, [please create an issue](https://github.com/azu/immutable-array-prototype/issues).
48 |
49 | 1. Fork it!
50 | 2. Create your feature branch: `git checkout -b my-new-feature`
51 | 3. Commit your changes: `git commit -am 'Add some feature'`
52 | 4. Push to the branch: `git push origin my-new-feature`
53 | 5. Submit a pull request :D
54 |
55 | ## Author
56 |
57 | - [github/azu](https://github.com/azu)
58 | - [twitter/azu_re](https://twitter.com/azu_re)
59 |
60 | ## License
61 |
62 | MIT © azu
63 |
--------------------------------------------------------------------------------
/packages/push/jest.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | "transform": {
3 | ".(ts|tsx)": "ts-jest"
4 | },
5 | "testRegex": "(./test/.*-(test|spec))\\.(ts|tsx|js)$",
6 | "testPathIgnorePatterns": ["/lib/", "/out/", "/node_modules/"],
7 | "moduleFileExtensions": [
8 | "ts",
9 | "tsx",
10 | "js"
11 | ]
12 | };
13 |
--------------------------------------------------------------------------------
/packages/push/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "directories": {
3 | "lib": "lib",
4 | "test": "test"
5 | },
6 | "author": "azu",
7 | "license": "MIT",
8 | "files": [
9 | "bin/",
10 | "lib/",
11 | "src/"
12 | ],
13 | "name": "@immutable-array/push",
14 | "version": "1.0.6",
15 | "description": "Immutable Array.prototype.push",
16 | "main": "lib/push.js",
17 | "types": "lib/push.d.ts",
18 | "scripts": {
19 | "build": "tsc -p .",
20 | "watch": "tsc -p . --watch",
21 | "prepublish": "npm run --if-present build",
22 | "test": "jest"
23 | },
24 | "publishConfig": {
25 | "access": "public"
26 | },
27 | "keywords": [
28 | "immutable",
29 | "array"
30 | ],
31 | "repository": {
32 | "type": "git",
33 | "url": "https://github.com/azu/immutable-array-prototype.git"
34 | },
35 | "bugs": {
36 | "url": "https://github.com/azu/immutable-array-prototype/issues"
37 | },
38 | "homepage": "https://github.com/azu/immutable-array-prototype/tree/master/packages/push/",
39 | "devDependencies": {
40 | "@types/jest": "^29.2.5",
41 | "@types/node": "^18.11.18",
42 | "jest": "^29.3.1",
43 | "ts-jest": "^29.0.3",
44 | "typescript": "^4.9.4"
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/packages/push/src/push.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * Appends new elements to an array, and returns the new length of the array.
3 | * @param array base Array
4 | * @param items New elements of the Array.
5 | */
6 | export function push(array: Array, ...items: T[]): Array {
7 | return array.concat(items);
8 | }
9 |
--------------------------------------------------------------------------------
/packages/push/test/push-test.ts:
--------------------------------------------------------------------------------
1 | // MIT © 2017 azu
2 | import { push } from "../src/push";
3 | import * as assert from "assert";
4 |
5 | describe("push", () => {
6 | it("can push element to empty array", () => {
7 | const originalArray: string[] = [];
8 | const resultArray = push(originalArray, "a");
9 | assert.deepStrictEqual(resultArray, ["a"]);
10 | });
11 | it("can push multiple elements", () => {
12 | const originalArray = ["a", "b", "c", "d", "e"];
13 | const addingArray = ["f", "g"];
14 | const resultArray = push(originalArray, ...addingArray);
15 | assert.deepStrictEqual(resultArray, ["a", "b", "c", "d", "e", "f", "g"]);
16 | });
17 | it("can push an array", () => {
18 | const originalArray = ["a", "b", "c", "d", "e"];
19 | const addingArray = ["f", "g"];
20 | const resultArray = push(originalArray, addingArray);
21 | assert.deepStrictEqual(resultArray, ["a", "b", "c", "d", "e", ["f", "g"]]);
22 | });
23 | it("should be idempotent - x() === x()", () => {
24 | const originalArray = ["a", "b", "c", "d", "e"];
25 | assert.deepStrictEqual(push(originalArray, 1), push(originalArray, 1));
26 | });
27 | it("should return empty array when adding empty element to empty array", () => {
28 | const originalArray: any[] = [];
29 | const resultArray = push(originalArray);
30 | assert.strictEqual(resultArray.length, 0);
31 | });
32 | it("should return immutable result array", () => {
33 | const originalArray = ["a", "b", "c", "d", "e"];
34 | const resultArray = push(originalArray, "f", "g");
35 | assert.ok(originalArray !== resultArray);
36 | });
37 | it("should return result array have length = original + adding", () => {
38 | const originalArray = ["a", "b", "c", "d", "e"];
39 | const addingArray = ["f", "g"];
40 | const resultArray = push(originalArray, ...addingArray);
41 | assert.strictEqual(resultArray.length, originalArray.length + addingArray.length);
42 | });
43 | });
44 |
--------------------------------------------------------------------------------
/packages/push/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": "../../tsconfig.json",
3 | "compilerOptions": {
4 | "outDir": "./lib/"
5 | },
6 | "include": [
7 | "src/**/*"
8 | ]
9 | }
10 |
--------------------------------------------------------------------------------
/packages/reverse/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Change Log
2 |
3 | All notable changes to this project will be documented in this file.
4 | See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5 |
6 |
7 | ## [1.0.4](https://github.com/azu/immutable-array-prototype/compare/v1.0.3...v1.0.4) (2018-03-22)
8 |
9 |
10 |
11 |
12 | **Note:** Version bump only for package @immutable-array/reverse
13 |
14 |
15 | ## 1.0.1 (2017-06-24)
16 |
17 |
18 | ### Features
19 |
20 | * **prototype:** add [@immutable](https://github.com/immutable)-array/prototype module ([#22](https://github.com/azu/immutable-array-prototype/issues/22)) ([86f9452](https://github.com/azu/immutable-array-prototype/commit/86f9452))
21 | * **reverse:** implement immutable Array#reverse ([33ad03b](https://github.com/azu/immutable-array-prototype/commit/33ad03b))
22 |
--------------------------------------------------------------------------------
/packages/reverse/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2017 azu
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy
4 | of this software and associated documentation files (the "Software"), to deal
5 | in the Software without restriction, including without limitation the rights
6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | copies of the Software, and to permit persons to whom the Software is
8 | furnished to do so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in all
11 | copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 | SOFTWARE.
20 |
--------------------------------------------------------------------------------
/packages/reverse/README.md:
--------------------------------------------------------------------------------
1 | # @immutable-array/reverse
2 |
3 | Immutable [`Array.prototype.reverse()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse).
4 |
5 | ## Install
6 |
7 | Install with [npm](https://www.npmjs.com/):
8 |
9 | npm install @immutable-array/reverse
10 |
11 | ## Usage
12 |
13 | Same API with [`Array.prototype.reverse()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse).
14 |
15 | ```ts
16 | /**
17 | * Reverses the elements in an Array.
18 | * @param array base array
19 | */
20 | export declare function reverse(array: Array): Array;
21 | ```
22 |
23 | ## Example
24 |
25 | ```js
26 | import { reverse } from "@immutable-array/reverse"
27 | assert.deepStrictEqual(reverse(["a", "b", "c", "d", "e"]), ["e", "d", "c", "b", "a"]);
28 | assert.deepStrictEqual(reverse([1, 2, 3, 4, 5]), [5, 4, 3, 2, 1]);
29 | assert.deepStrictEqual(reverse([true, Infinity, undefined, undefined, "NaN", "-1"]), [
30 | "-1",
31 | "NaN",
32 | undefined,
33 | undefined,
34 | Infinity,
35 | true
36 | ]);
37 | ```
38 |
39 | ## Changelog
40 |
41 | See [Releases page](https://github.com/azu/immutable-array-prototype/releases).
42 |
43 | ## Running tests
44 |
45 | Install devDependencies and Run `npm test`:
46 |
47 | npm i -d && npm test
48 |
49 | ## Contributing
50 |
51 | Pull requests and stars are always welcome.
52 |
53 | For bugs and feature requests, [please create an issue](https://github.com/azu/immutable-array-prototype/issues).
54 |
55 | 1. Fork it!
56 | 2. Create your feature branch: `git checkout -b my-new-feature`
57 | 3. Commit your changes: `git commit -am 'Add some feature'`
58 | 4. Push to the branch: `git push origin my-new-feature`
59 | 5. Submit a pull request :D
60 |
61 | ## Author
62 |
63 | - [github/azu](https://github.com/azu)
64 | - [twitter/azu_re](https://twitter.com/azu_re)
65 |
66 | ## License
67 |
68 | MIT © azu
69 |
--------------------------------------------------------------------------------
/packages/reverse/jest.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | "transform": {
3 | ".(ts|tsx)": "ts-jest"
4 | },
5 | "testRegex": "(./test/.*-(test|spec))\\.(ts|tsx|js)$",
6 | "testPathIgnorePatterns": ["/lib/", "/out/", "/node_modules/"],
7 | "moduleFileExtensions": [
8 | "ts",
9 | "tsx",
10 | "js"
11 | ]
12 | };
13 |
--------------------------------------------------------------------------------
/packages/reverse/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "directories": {
3 | "lib": "lib",
4 | "test": "test"
5 | },
6 | "author": "azu",
7 | "license": "MIT",
8 | "files": [
9 | "bin/",
10 | "lib/",
11 | "src/"
12 | ],
13 | "name": "@immutable-array/reverse",
14 | "version": "1.0.6",
15 | "description": "Immutable Array.prototype.reverse",
16 | "main": "lib/reverse.js",
17 | "types": "lib/reverse.d.ts",
18 | "scripts": {
19 | "build": "tsc -p .",
20 | "watch": "tsc -p . --watch",
21 | "prepublish": "npm run --if-present build",
22 | "test": "jest"
23 | },
24 | "publishConfig": {
25 | "access": "public"
26 | },
27 | "keywords": [
28 | "array",
29 | "immutable"
30 | ],
31 | "repository": {
32 | "type": "git",
33 | "url": "https://github.com/azu/immutable-array-prototype.git"
34 | },
35 | "bugs": {
36 | "url": "https://github.com/azu/immutable-array-prototype/issues"
37 | },
38 | "homepage": "https://github.com/azu/immutable-array-prototype/tree/master/packages/reverse/",
39 | "devDependencies": {
40 | "@types/jest": "^29.2.5",
41 | "@types/node": "^18.11.18",
42 | "jest": "^29.3.1",
43 | "ts-jest": "^29.0.3",
44 | "typescript": "^4.9.4"
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/packages/reverse/src/reverse.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * Reverses the elements in an Array.
3 | * @param array base array
4 | */
5 | export function reverse(array: Array): Array {
6 | return [...array].reverse();
7 | }
8 |
--------------------------------------------------------------------------------
/packages/reverse/test/reverse-test.ts:
--------------------------------------------------------------------------------
1 | import { reverse } from "../src/reverse";
2 | import * as assert from "assert";
3 |
4 | describe("reverse", () => {
5 | it("should return immutable array", () => {
6 | const originalArray = ["a", "b", "c", "d", "e"];
7 | const resultArray = reverse(originalArray);
8 | assert.ok(originalArray !== resultArray);
9 | });
10 |
11 | it("can reverse empty array", () => {
12 | assert.deepStrictEqual(reverse([]), []);
13 | });
14 |
15 | it("should be idempotent - x() === x()", () => {
16 | const originalArray = ["a", "b", "c", "d", "e"];
17 | assert.deepStrictEqual(reverse(originalArray), reverse(originalArray));
18 | });
19 |
20 | it("should reverse about same value", () => {
21 | const a = {},
22 | b = {};
23 | assert.deepStrictEqual(reverse([a, b]), [b, a]);
24 | });
25 |
26 | it("should reverse order the array.", () => {
27 | assert.deepStrictEqual(reverse(["a", "b", "c", "d", "e"]), ["e", "d", "c", "b", "a"]);
28 | assert.deepStrictEqual(reverse([1, 2, 3, 4, 5]), [5, 4, 3, 2, 1]);
29 | assert.deepStrictEqual(reverse([true, Infinity, undefined, undefined, "NaN", "-1"]), [
30 | "-1",
31 | "NaN",
32 | undefined,
33 | undefined,
34 | Infinity,
35 | true
36 | ]);
37 | });
38 | });
39 |
--------------------------------------------------------------------------------
/packages/reverse/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": "../../tsconfig.json",
3 | "compilerOptions": {
4 | "outDir": "./lib/"
5 | },
6 | "include": [
7 | "src/**/*"
8 | ]
9 | }
10 |
--------------------------------------------------------------------------------
/packages/shift/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Change Log
2 |
3 | All notable changes to this project will be documented in this file.
4 | See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5 |
6 |
7 | ## [1.0.4](https://github.com/azu/immutable-array-prototype/compare/v1.0.3...v1.0.4) (2018-03-22)
8 |
9 |
10 |
11 |
12 | **Note:** Version bump only for package @immutable-array/shift
13 |
14 |
15 | ## 1.0.1 (2017-06-24)
16 |
17 |
18 | ### Features
19 |
20 | * **prototype:** add [@immutable](https://github.com/immutable)-array/prototype module ([#22](https://github.com/azu/immutable-array-prototype/issues/22)) ([86f9452](https://github.com/azu/immutable-array-prototype/commit/86f9452))
21 | * **shift:** implement immutable Array#shift ([#14](https://github.com/azu/immutable-array-prototype/issues/14)) ([f7feda9](https://github.com/azu/immutable-array-prototype/commit/f7feda9))
22 |
--------------------------------------------------------------------------------
/packages/shift/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2017 azu
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy
4 | of this software and associated documentation files (the "Software"), to deal
5 | in the Software without restriction, including without limitation the rights
6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | copies of the Software, and to permit persons to whom the Software is
8 | furnished to do so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in all
11 | copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 | SOFTWARE.
20 |
--------------------------------------------------------------------------------
/packages/shift/README.md:
--------------------------------------------------------------------------------
1 | # @immutable-array/shift
2 |
3 | Immutable [`Array.prototype.shift()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/shift).
4 |
5 | ## Install
6 |
7 | Install with [npm](https://www.npmjs.com/):
8 |
9 | npm install @immutable-array/shift
10 |
11 | ## Usage
12 |
13 | Same API with [`Array.prototype.shift()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/shift).
14 |
15 | ```ts
16 | /**
17 | * Removes the first element from an array and returns it.
18 | */
19 | export declare function shift(array: Array): Array;
20 | ```
21 |
22 | ## Example
23 |
24 | ```js
25 | import { shift } from "@immutable-array/shift"
26 | const originalArray = ["a", "b", "c", "d", "e"];
27 | const resultArray = shift(originalArray);
28 | assert.deepStrictEqual(resultArray, ["b", "c", "d", "e"]);
29 | ```
30 |
31 | ## Changelog
32 |
33 | See [Releases page](https://github.com/azu/immutable-array-prototype/releases).
34 |
35 | ## Running tests
36 |
37 | Install devDependencies and Run `npm test`:
38 |
39 | npm i -d && npm test
40 |
41 | ## Contributing
42 |
43 | Pull requests and stars are always welcome.
44 |
45 | For bugs and feature requests, [please create an issue](https://github.com/azu/immutable-array-prototype/issues).
46 |
47 | 1. Fork it!
48 | 2. Create your feature branch: `git checkout -b my-new-feature`
49 | 3. Commit your changes: `git commit -am 'Add some feature'`
50 | 4. Push to the branch: `git push origin my-new-feature`
51 | 5. Submit a pull request :D
52 |
53 | ## Author
54 |
55 | - [github/azu](https://github.com/azu)
56 | - [twitter/azu_re](https://twitter.com/azu_re)
57 |
58 | ## License
59 |
60 | MIT © azu
61 |
--------------------------------------------------------------------------------
/packages/shift/jest.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | "transform": {
3 | ".(ts|tsx)": "ts-jest"
4 | },
5 | "testRegex": "(./test/.*-(test|spec))\\.(ts|tsx|js)$",
6 | "testPathIgnorePatterns": ["/lib/", "/out/", "/node_modules/"],
7 | "moduleFileExtensions": [
8 | "ts",
9 | "tsx",
10 | "js"
11 | ]
12 | };
13 |
--------------------------------------------------------------------------------
/packages/shift/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "directories": {
3 | "lib": "lib",
4 | "test": "test"
5 | },
6 | "author": "azu",
7 | "license": "MIT",
8 | "files": [
9 | "bin/",
10 | "lib/",
11 | "src/"
12 | ],
13 | "name": "@immutable-array/shift",
14 | "version": "1.0.6",
15 | "description": "Immutable Array.prototype.shift.",
16 | "main": "lib/shift.js",
17 | "types": "lib/shift.d.ts",
18 | "scripts": {
19 | "build": "tsc -p .",
20 | "watch": "tsc -p . --watch",
21 | "prepublish": "npm run --if-present build",
22 | "test": "jest"
23 | },
24 | "publishConfig": {
25 | "access": "public"
26 | },
27 | "keywords": [
28 | "array",
29 | "immutable"
30 | ],
31 | "repository": {
32 | "type": "git",
33 | "url": "https://github.com/azu/immutable-array-prototype.git"
34 | },
35 | "bugs": {
36 | "url": "https://github.com/azu/immutable-array-prototype/issues"
37 | },
38 | "homepage": "https://github.com/azu/immutable-array-prototype/tree/master/packages/shift/",
39 | "devDependencies": {
40 | "@types/jest": "^29.2.5",
41 | "@types/node": "^18.11.18",
42 | "jest": "^29.3.1",
43 | "ts-jest": "^29.0.3",
44 | "typescript": "^4.9.4"
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/packages/shift/src/shift.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * Removes the first element from an array and returns it.
3 | */
4 | export function shift(array: Array): Array {
5 | return array.slice(1);
6 | }
7 |
--------------------------------------------------------------------------------
/packages/shift/test/shift-test.ts:
--------------------------------------------------------------------------------
1 | // MIT © 2017 azu
2 | import { shift } from "../src/shift";
3 | import * as assert from "assert";
4 |
5 | describe("shift", () => {
6 | it("should return empty array when shift empty array", () => {
7 | const originalArray: any[] = [];
8 | const resultArray = shift(originalArray);
9 | assert.deepStrictEqual(resultArray, []);
10 | });
11 | it("should return shifted array", () => {
12 | const originalArray = ["a", "b", "c", "d", "e"];
13 | const resultArray = shift(originalArray);
14 | assert.deepStrictEqual(resultArray, ["b", "c", "d", "e"]);
15 | });
16 | it("should return immutable result array", () => {
17 | const originalArray = ["a", "b", "c", "d", "e"];
18 | const resultArray = shift(originalArray);
19 | assert.ok(originalArray !== resultArray);
20 | });
21 | it("should be idempotent - x() === x()", () => {
22 | const originalArray = ["a", "b", "c", "d", "e"];
23 | assert.deepStrictEqual(shift(originalArray), shift(originalArray));
24 | });
25 | });
26 |
--------------------------------------------------------------------------------
/packages/shift/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": "../../tsconfig.json",
3 | "compilerOptions": {
4 | "outDir": "./lib/"
5 | },
6 | "include": [
7 | "src/**/*"
8 | ]
9 | }
10 |
--------------------------------------------------------------------------------
/packages/sort/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Change Log
2 |
3 | All notable changes to this project will be documented in this file.
4 | See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5 |
6 |
7 | ## [1.0.4](https://github.com/azu/immutable-array-prototype/compare/v1.0.3...v1.0.4) (2018-03-22)
8 |
9 |
10 |
11 |
12 | **Note:** Version bump only for package @immutable-array/sort
13 |
14 |
15 | ## 1.0.1 (2017-06-24)
16 |
17 |
18 | ### Features
19 |
20 | * **prototype:** add [@immutable](https://github.com/immutable)-array/prototype module ([#22](https://github.com/azu/immutable-array-prototype/issues/22)) ([86f9452](https://github.com/azu/immutable-array-prototype/commit/86f9452))
21 | * **sort:** implement immutable Array#sort ([#18](https://github.com/azu/immutable-array-prototype/issues/18)) ([ebd7095](https://github.com/azu/immutable-array-prototype/commit/ebd7095))
22 |
--------------------------------------------------------------------------------
/packages/sort/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2017 azu
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy
4 | of this software and associated documentation files (the "Software"), to deal
5 | in the Software without restriction, including without limitation the rights
6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | copies of the Software, and to permit persons to whom the Software is
8 | furnished to do so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in all
11 | copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 | SOFTWARE.
20 |
--------------------------------------------------------------------------------
/packages/sort/README.md:
--------------------------------------------------------------------------------
1 | # @immutable-array/sort
2 |
3 | Immutable [`Array.prototype.sort()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/sort).
4 |
5 | ## Install
6 |
7 | Install with [npm](https://www.npmjs.com/):
8 |
9 | npm install @immutable-array/sort
10 |
11 | ## Usage
12 |
13 | Same API with [`Array.prototype.sort()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/sort).
14 |
15 | ```ts
16 | /**
17 | * Sorts an array.
18 | * @param array Base array
19 | * @param compareFn The name of the function used to determine the order of the elements. If omitted, the elements are sorted in ascending, ASCII character order.
20 | */
21 | export declare function sort(array: Array, compareFn?: (a: T, b: T) => number): Array;
22 | ```
23 |
24 | ## Example
25 |
26 | Default sort:
27 |
28 | ```js
29 | import { sort } from "@immutable-array/sort"
30 | assert.deepStrictEqual(sort(["a", "b", "c", "d", "e"]), ["a", "b", "c", "d", "e"]);
31 | assert.deepStrictEqual(sort(["e", "a", "c", "b", "d"]), ["a", "b", "c", "d", "e"]);
32 | assert.deepStrictEqual(sort([1, 5, 2, 4, 3]), [1, 2, 3, 4, 5]);
33 | assert.deepStrictEqual(sort([5, 2, 4, 3, 1]), [1, 2, 3, 4, 5]);
34 | assert.deepStrictEqual(sort(["1", "10", "2"]), ["1", "10", "2"]);
35 | assert.deepStrictEqual(sort(["10", "2", "1"]), ["1", "10", "2"]);
36 | ```
37 |
38 | Custom sort:
39 |
40 | ```js
41 | import { sort } from "@immutable-array/sort"
42 | const numberArray = [20, 3, 4, 10, -3, 1, 0, 5];
43 | const resultArray = sort(numberArray, (a, b) => b - a);
44 | assert.deepStrictEqual(resultArray, [20, 10, 5, 4, 3, 1, 0, -3]);
45 | ```
46 |
47 |
48 | ## Changelog
49 |
50 | See [Releases page](https://github.com/azu/immutable-array-prototype/releases).
51 |
52 | ## Running tests
53 |
54 | Install devDependencies and Run `npm test`:
55 |
56 | npm i -d && npm test
57 |
58 | ## Contributing
59 |
60 | Pull requests and stars are always welcome.
61 |
62 | For bugs and feature requests, [please create an issue](https://github.com/azu/immutable-array-prototype/issues).
63 |
64 | 1. Fork it!
65 | 2. Create your feature branch: `git checkout -b my-new-feature`
66 | 3. Commit your changes: `git commit -am 'Add some feature'`
67 | 4. Push to the branch: `git push origin my-new-feature`
68 | 5. Submit a pull request :D
69 |
70 | ## Author
71 |
72 | - [github/azu](https://github.com/azu)
73 | - [twitter/azu_re](https://twitter.com/azu_re)
74 |
75 | ## License
76 |
77 | MIT © azu
78 |
--------------------------------------------------------------------------------
/packages/sort/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "directories": {
3 | "lib": "lib",
4 | "test": "test"
5 | },
6 | "author": "azu",
7 | "license": "MIT",
8 | "files": [
9 | "bin/",
10 | "lib/",
11 | "src/"
12 | ],
13 | "name": "@immutable-array/sort",
14 | "version": "1.0.6",
15 | "description": "Immutable Array.prototype.sort.",
16 | "main": "lib/sort.js",
17 | "types": "lib/sort.d.ts",
18 | "scripts": {
19 | "build": "tsc -p .",
20 | "watch": "tsc -p . --watch",
21 | "prepublish": "npm run --if-present build",
22 | "test": "jest"
23 | },
24 | "publishConfig": {
25 | "access": "public"
26 | },
27 | "keywords": [
28 | "array",
29 | "immutable"
30 | ],
31 | "repository": {
32 | "type": "git",
33 | "url": "https://github.com/azu/immutable-array-prototype.git"
34 | },
35 | "bugs": {
36 | "url": "https://github.com/azu/immutable-array-prototype/issues"
37 | },
38 | "homepage": "https://github.com/azu/immutable-array-prototype/tree/master/packages/sort/",
39 | "devDependencies": {
40 | "@types/jest": "^29.2.5",
41 | "@types/node": "^18.11.18",
42 | "jest": "^29.3.1",
43 | "ts-jest": "^29.0.3",
44 | "typescript": "^4.9.4"
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/packages/sort/src/sort.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * Sorts an array.
3 | * @param array Base array
4 | * @param compareFn The name of the function used to determine the order of the elements. If omitted, the elements are sorted in ascending, ASCII character order.
5 | */
6 | export function sort(array: Array, compareFn?: (a: T, b: T) => number): Array {
7 | return [...array].sort(compareFn);
8 | }
9 |
--------------------------------------------------------------------------------
/packages/sort/test/sort-test.ts:
--------------------------------------------------------------------------------
1 | // MIT © 2017 azu
2 | import { sort } from "../src/sort";
3 | import * as assert from "assert";
4 |
5 | describe("sort", () => {
6 | it("should return immutable array", () => {
7 | const originalArray = ["a", "b", "c", "d", "e"];
8 | const resultArray = sort(originalArray);
9 | assert.ok(originalArray !== resultArray);
10 | });
11 |
12 | it("can sort empty array", () => {
13 | assert.deepStrictEqual(sort([]), []);
14 | });
15 |
16 | it("should be idempotent - x() === x()", () => {
17 | const originalArray = ["a", "b", "c", "d", "e"];
18 | assert.deepStrictEqual(sort(originalArray), sort(originalArray));
19 | });
20 | describe("default sort", () => {
21 | it("should sort in ascending by ASCII character order.", () => {
22 | assert.deepStrictEqual(sort(["a", "b", "c", "d", "e"]), ["a", "b", "c", "d", "e"]);
23 | assert.deepStrictEqual(sort(["e", "a", "c", "b", "d"]), ["a", "b", "c", "d", "e"]);
24 | assert.deepStrictEqual(sort([1, 5, 2, 4, 3]), [1, 2, 3, 4, 5]);
25 | assert.deepStrictEqual(sort([5, 2, 4, 3, 1]), [1, 2, 3, 4, 5]);
26 | assert.deepStrictEqual(sort(["1", "10", "2"]), ["1", "10", "2"]);
27 | assert.deepStrictEqual(sort(["10", "2", "1"]), ["1", "10", "2"]);
28 | });
29 | });
30 | describe("custom sort", () => {
31 | it("sorts numeric array (ascending)", () => {
32 | // https://github.com/georapbox/immutable-arrays/blob/master/test/immutable-sort.spec.js
33 | const numberArray = [20, 3, 4, 10, -3, 1, 0, 5];
34 | const resultArray = sort(numberArray, (a, b) => a - b);
35 | assert.deepStrictEqual(resultArray, [-3, 0, 1, 3, 4, 5, 10, 20]);
36 | });
37 |
38 | it("sorts numeric array (descending)", () => {
39 | const numberArray = [20, 3, 4, 10, -3, 1, 0, 5];
40 | const resultArray = sort(numberArray, (a, b) => b - a);
41 | assert.deepStrictEqual(resultArray, [20, 10, 5, 4, 3, 1, 0, -3]);
42 | });
43 |
44 | it("sorts string array (with compareFunction) (descending)", () => {
45 | const stringArray = ["Blue", "Humpback", "Beluga"];
46 | const resultArray = sort(stringArray, (a, b) => a.toLowerCase() < b.toLowerCase());
47 | assert.deepStrictEqual(resultArray, ["Humpback", "Blue", "Beluga"]);
48 | });
49 | });
50 | });
51 |
--------------------------------------------------------------------------------
/packages/sort/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": "../../tsconfig.json",
3 | "compilerOptions": {
4 | "outDir": "./lib/"
5 | },
6 | "include": [
7 | "src/**/*"
8 | ]
9 | }
10 |
--------------------------------------------------------------------------------
/packages/splice/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Change Log
2 |
3 | All notable changes to this project will be documented in this file.
4 | See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5 |
6 |
7 | ## [1.0.4](https://github.com/azu/immutable-array-prototype/compare/v1.0.3...v1.0.4) (2018-03-22)
8 |
9 |
10 |
11 |
12 | **Note:** Version bump only for package @immutable-array/splice
13 |
14 |
15 | ## 1.0.2 (2017-06-24)
16 |
17 |
18 | ### Bug Fixes
19 |
20 | * **splice:** fix splice start:-1 issue ([aafdd7c](https://github.com/azu/immutable-array-prototype/commit/aafdd7c))
21 |
22 |
23 |
24 |
25 | ## 1.0.1 (2017-06-24)
26 |
27 |
28 | ### Features
29 |
30 | * **fill:** implement immutable Array#fill ([#17](https://github.com/azu/immutable-array-prototype/issues/17)) ([6b3b7d5](https://github.com/azu/immutable-array-prototype/commit/6b3b7d5))
31 | * **prototype:** add [@immutable](https://github.com/immutable)-array/prototype module ([#22](https://github.com/azu/immutable-array-prototype/issues/22)) ([86f9452](https://github.com/azu/immutable-array-prototype/commit/86f9452))
32 | * **splice:** implement immutable Array#splice ([#16](https://github.com/azu/immutable-array-prototype/issues/16)) ([3e48d9d](https://github.com/azu/immutable-array-prototype/commit/3e48d9d))
33 |
34 |
35 |
36 |
37 |
38 | ## 1.0.1 (2017-06-24)
39 |
40 |
41 | ### Features
42 |
43 | * **fill:** implement immutable Array#fill ([#17](https://github.com/azu/immutable-array-prototype/issues/17)) ([6b3b7d5](https://github.com/azu/immutable-array-prototype/commit/6b3b7d5))
44 | * **prototype:** add [@immutable](https://github.com/immutable)-array/prototype module ([#22](https://github.com/azu/immutable-array-prototype/issues/22)) ([86f9452](https://github.com/azu/immutable-array-prototype/commit/86f9452))
45 | * **splice:** implement immutable Array#splice ([#16](https://github.com/azu/immutable-array-prototype/issues/16)) ([3e48d9d](https://github.com/azu/immutable-array-prototype/commit/3e48d9d))
46 |
--------------------------------------------------------------------------------
/packages/splice/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2017 azu
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy
4 | of this software and associated documentation files (the "Software"), to deal
5 | in the Software without restriction, including without limitation the rights
6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | copies of the Software, and to permit persons to whom the Software is
8 | furnished to do so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in all
11 | copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 | SOFTWARE.
20 |
--------------------------------------------------------------------------------
/packages/splice/README.md:
--------------------------------------------------------------------------------
1 | # @immutable-array/splice
2 |
3 | Immutable [`Array.prototype.splice()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/splice).
4 |
5 | ## Install
6 |
7 | Install with [npm](https://www.npmjs.com/):
8 |
9 | npm install @immutable-array/splice
10 |
11 | ## Usage
12 |
13 | Same API with [`Array.prototype.splice()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/splice).
14 |
15 | ```ts
16 | /**
17 | * Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
18 | * @param array Base array.
19 | * @param start The zero-based location in the array from which to start removing elements.
20 | * @param deleteCount The number of elements to remove.
21 | * @param items Elements to insert into the array in place of the deleted elements.
22 | */
23 | export declare function splice(array: Array, start?: number, deleteCount?: number, ...items: T[]): Array;
24 | ```
25 |
26 | ### Example
27 |
28 | Delete all:
29 |
30 | ```js
31 | import { splice } from "@immutable-array/splice"
32 | const originalArray = ["a", "b", "c", "d", "e"];
33 | const resultArray = splice(originalArray, 0);
34 | assert.deepStrictEqual(resultArray, []);
35 | ```
36 |
37 | Delete range: 1-3
38 |
39 | ```js
40 | import { splice } from "@immutable-array/splice"
41 | const originalArray = ["a", "b", "c", "d", "e"];
42 | const resultArray = splice(originalArray, 1, 3);
43 | assert.deepStrictEqual(resultArray, ["a", "e"]);
44 | ```
45 |
46 | Delete and Insert = replace index 0 with "x"
47 |
48 | ```js
49 | import { splice } from "@immutable-array/splice"
50 | const originalArray = ["a", "b", "c", "d", "e"];
51 | const resultArray = splice(originalArray, 0, 1, "x");
52 | assert.deepStrictEqual(resultArray, ["x", "b", "c", "d", "e"]);
53 | ```
54 |
55 | ## Changelog
56 |
57 | See [Releases page](https://github.com/azu/immutable-array-prototype/releases).
58 |
59 | ## Running tests
60 |
61 | Install devDependencies and Run `npm test`:
62 |
63 | npm i -d && npm test
64 |
65 | ## Contributing
66 |
67 | Pull requests and stars are always welcome.
68 |
69 | For bugs and feature requests, [please create an issue](https://github.com/azu/immutable-array-prototype/issues).
70 |
71 | 1. Fork it!
72 | 2. Create your feature branch: `git checkout -b my-new-feature`
73 | 3. Commit your changes: `git commit -am 'Add some feature'`
74 | 4. Push to the branch: `git push origin my-new-feature`
75 | 5. Submit a pull request :D
76 |
77 | ## Author
78 |
79 | - [github/azu](https://github.com/azu)
80 | - [twitter/azu_re](https://twitter.com/azu_re)
81 |
82 | ## License
83 |
84 | MIT © azu
85 |
--------------------------------------------------------------------------------
/packages/splice/jest.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | "transform": {
3 | ".(ts|tsx)": "ts-jest"
4 | },
5 | "testRegex": "(./test/.*-(test|spec))\\.(ts|tsx|js)$",
6 | "testPathIgnorePatterns": ["/lib/", "/out/", "/node_modules/"],
7 | "moduleFileExtensions": [
8 | "ts",
9 | "tsx",
10 | "js"
11 | ]
12 | };
13 |
--------------------------------------------------------------------------------
/packages/splice/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "directories": {
3 | "lib": "lib",
4 | "test": "test"
5 | },
6 | "author": "azu",
7 | "license": "MIT",
8 | "files": [
9 | "bin/",
10 | "lib/",
11 | "src/"
12 | ],
13 | "name": "@immutable-array/splice",
14 | "version": "1.0.6",
15 | "description": "Immutable Array.prototype.splice",
16 | "main": "lib/splice.js",
17 | "types": "lib/splice.d.ts",
18 | "scripts": {
19 | "build": "tsc -p .",
20 | "watch": "tsc -p . --watch",
21 | "prepublish": "npm run --if-present build",
22 | "test": "jest"
23 | },
24 | "publishConfig": {
25 | "access": "public"
26 | },
27 | "keywords": [
28 | "array",
29 | "immutable"
30 | ],
31 | "repository": {
32 | "type": "git",
33 | "url": "https://github.com/azu/immutable-array-prototype.git"
34 | },
35 | "bugs": {
36 | "url": "https://github.com/azu/immutable-array-prototype/issues"
37 | },
38 | "homepage": "https://github.com/azu/immutable-array-prototype/tree/master/packages/splice/",
39 | "devDependencies": {
40 | "@types/jest": "^29.2.5",
41 | "@types/node": "^18.11.18",
42 | "jest": "^29.3.1",
43 | "ts-jest": "^29.0.3",
44 | "typescript": "^4.9.4"
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/packages/splice/src/splice.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
3 | * @param array Base array.
4 | * @param start The zero-based location in the array from which to start removing elements.
5 | * @param deleteCount The number of elements to remove.
6 | * @param items Elements to insert into the array in place of the deleted elements.
7 | */
8 | export function splice(array: Array, start?: number, deleteCount?: number, ...items: T[]): Array;
9 | export function splice(array: Array, ...args: any[]): Array {
10 | const copiedArray = Array.prototype.slice.call(array);
11 | // @ts-ignore
12 | Array.prototype.splice.apply(copiedArray, args);
13 | return copiedArray;
14 | }
15 |
--------------------------------------------------------------------------------
/packages/splice/test/splice-test.ts:
--------------------------------------------------------------------------------
1 | // MIT © 2017 azu
2 | import { splice } from "../src/splice";
3 | import * as assert from "assert";
4 |
5 | describe("splice", () => {
6 | it("should return immutable array", () => {
7 | const originalArray = ["a", "b", "c", "d", "e"];
8 | const resultArray = splice(originalArray, 0, 1, "x");
9 | assert.ok(originalArray !== resultArray);
10 | });
11 |
12 | it("should return array that has same element with original: start:0 delete: 0", () => {
13 | const originalArray = ["a", "b", "c", "d", "e"];
14 | const resultArray = splice(originalArray, 0, 0);
15 | assert.deepStrictEqual(resultArray, originalArray);
16 | });
17 |
18 | describe("delete pattern", () => {
19 | it("should remove all elements from array, start:0", () => {
20 | const originalArray = ["a", "b", "c", "d", "e"];
21 | const resultArray = splice(originalArray, 0);
22 | assert.deepStrictEqual(resultArray, []);
23 | });
24 | it("should remove all elements from array, start:0 delete:length", () => {
25 | const originalArray = ["a", "b", "c", "d", "e"];
26 | const resultArray = splice(originalArray, 0, originalArray.length);
27 | assert.deepStrictEqual(resultArray, []);
28 | });
29 | it("should remove all elements from array, start:0 delete:length + 1", () => {
30 | const originalArray = ["a", "b", "c", "d", "e"];
31 | const resultArray = splice(originalArray, 0, originalArray.length + 1);
32 | assert.deepStrictEqual(resultArray, []);
33 | });
34 | it("should remove last-index -2 element from array, start:-2 delete:1", () => {
35 | const originalArray = ["a", "b", "c", "d", "e"];
36 | const resultArray = splice(originalArray, -2, 1);
37 | assert.deepStrictEqual(resultArray, ["a", "b", "c", "e"]);
38 | });
39 | it("should not remove element from array, start:0 delete:-1", () => {
40 | // If deleteCount is negative, use 0
41 | // https://github.com/tc39/test262/blob/da4f4385fdf88ff2c8acf036efaaa62f8cd6bd58/test/built-ins/Array/prototype/splice/S15.4.4.12_A1.3_T1.js
42 | const originalArray = ["a", "b", "c", "d", "e"];
43 | const resultArray = splice(originalArray, 0, -1);
44 | assert.deepStrictEqual(resultArray, ["a", "b", "c", "d", "e"]);
45 | });
46 | it("should remove all elements from array, start:-2 delete:-1", () => {
47 | // ref: https://github.com/tc39/test262/blob/da4f4385fdf88ff2c8acf036efaaa62f8cd6bd58/test/built-ins/Array/prototype/splice/S15.4.4.12_A1.2_T1.js
48 | const originalArray = [0, 1];
49 | const resultArray = splice(originalArray, -2, -1);
50 | assert.deepStrictEqual(resultArray, [0, 1]);
51 | });
52 | it("should remove 3 elements from array, start:1 delete:3", () => {
53 | const originalArray = ["a", "b", "c", "d", "e"];
54 | const resultArray = splice(originalArray, 1, 3);
55 | assert.deepStrictEqual(resultArray, ["a", "e"]);
56 | });
57 | it("should not remove elements from array, when no arguments", () => {
58 | const originalArray = ["a", "b", "c", "d", "e"];
59 | const resultArray = splice(originalArray);
60 | assert.deepStrictEqual(resultArray, ["a", "b", "c", "d", "e"]);
61 | });
62 | });
63 | describe("insert pattern", () => {
64 | it('should return array that first element replaced with "x"', () => {
65 | const originalArray = ["a", "b", "c", "d", "e"];
66 | const resultArray = splice(originalArray, 0, 1, "x");
67 | assert.deepStrictEqual(resultArray, ["x", "b", "c", "d", "e"]);
68 | });
69 | it("should return array that inserted element at index 2", () => {
70 | const originalArray = ["a", "b", "c", "d", "e"];
71 | const resultArray = splice(originalArray, 2, 0, "x");
72 | assert.deepStrictEqual(resultArray, ["a", "b", "x", "c", "d", "e"]);
73 | });
74 | it("should return array that inserted element at last-index", () => {
75 | const originalArray = ["a", "b", "c", "d", "e"];
76 | const resultArray = splice(originalArray, originalArray.length, 0, "x");
77 | assert.deepStrictEqual(resultArray, ["a", "b", "c", "d", "e", "x"]);
78 | });
79 |
80 | it("should return array that inserted multiple elements", () => {
81 | const originalArray = ["a", "b", "c", "d", "e"];
82 | const addingArray = ["x", "y", "z"];
83 | const resultArray = splice(originalArray, 1, 0, ...addingArray);
84 | assert.deepStrictEqual(resultArray, ["a", "x", "y", "z", "b", "c", "d", "e"]);
85 | });
86 |
87 | it("should return array that inserted empty string", () => {
88 | const originalArray = ["a", "b", "c", "d", "e"];
89 | const resultArray = splice(originalArray, 0, 0, "");
90 | assert.deepStrictEqual(resultArray, ["", "a", "b", "c", "d", "e"]);
91 | });
92 | it("should return array that inserted undefined value", () => {
93 | const originalArray = ["a", "b", "c", "d", "e"];
94 | const resultArray = splice(originalArray, 0, 0, undefined);
95 | assert.deepStrictEqual(resultArray, [undefined, "a", "b", "c", "d", "e"]);
96 | });
97 | it("should return array that insert element and delete element - total number is not changed", () => {
98 | // This is often implementation issue
99 | // Most immutable array implementation not pass this test
100 | // https://github.com/micro-js/splice
101 | // https://vincent.billey.me/pure-javascript-immutable-array/
102 | // https://github.com/georapbox/immutable-arrays
103 | // For example, `indexOf` return -1 and use is as arguments, then occur this issue.
104 | const originalArray = [0, 1, 2];
105 | const resultArray = splice(originalArray, -1, 1, "x");
106 | assert.deepStrictEqual(resultArray, [0, 1, "x"]);
107 | assert.strictEqual(originalArray.length, resultArray.length);
108 | // same with native
109 | originalArray.splice(-1, 1, "x" as any);
110 | assert.deepStrictEqual(originalArray, resultArray);
111 | });
112 | });
113 | });
114 |
--------------------------------------------------------------------------------
/packages/splice/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": "../../tsconfig.json",
3 | "compilerOptions": {
4 | "outDir": "./lib/"
5 | },
6 | "include": [
7 | "src/**/*"
8 | ]
9 | }
10 |
--------------------------------------------------------------------------------
/packages/unshift/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Change Log
2 |
3 | All notable changes to this project will be documented in this file.
4 | See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5 |
6 |
7 | ## [1.0.4](https://github.com/azu/immutable-array-prototype/compare/v1.0.3...v1.0.4) (2018-03-22)
8 |
9 |
10 |
11 |
12 | **Note:** Version bump only for package @immutable-array/unshift
13 |
14 |
15 | ## 1.0.1 (2017-06-24)
16 |
17 |
18 | ### Features
19 |
20 | * **prototype:** add [@immutable](https://github.com/immutable)-array/prototype module ([#22](https://github.com/azu/immutable-array-prototype/issues/22)) ([86f9452](https://github.com/azu/immutable-array-prototype/commit/86f9452))
21 | * **unshift:** implement immutable Array#unshift ([#15](https://github.com/azu/immutable-array-prototype/issues/15)) ([d7a5ebd](https://github.com/azu/immutable-array-prototype/commit/d7a5ebd))
22 |
--------------------------------------------------------------------------------
/packages/unshift/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2017 azu
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy
4 | of this software and associated documentation files (the "Software"), to deal
5 | in the Software without restriction, including without limitation the rights
6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | copies of the Software, and to permit persons to whom the Software is
8 | furnished to do so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in all
11 | copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 | SOFTWARE.
20 |
--------------------------------------------------------------------------------
/packages/unshift/README.md:
--------------------------------------------------------------------------------
1 | # @immutable-array/unshift
2 |
3 | Immutable [`Array.prototype.unshift()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift).
4 |
5 | ## Install
6 |
7 | Install with [npm](https://www.npmjs.com/):
8 |
9 | npm install @immutable-array/unshift
10 |
11 | ## Usage
12 |
13 | Same API with [`Array.prototype.unshift()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift).
14 |
15 | ```ts
16 | /**
17 | * Inserts new elements at the start of an array.
18 | * @param array base Array.
19 | * @param items Elements to insert at the start of the Array.
20 | */
21 | export declare function unshift(array: Array, ...items: T[]): Array;
22 | ```
23 |
24 | ### Example
25 |
26 | ```js
27 | import { unshift } from "@immutable-array/unshift"
28 | const originalArray = ["a", "b", "c", "d", "e"];
29 | const addingArray = ["f", "g"];
30 | const resultArray = unshift(originalArray, ...addingArray);
31 | assert.deepStrictEqual(resultArray, ["f", "g", "a", "b", "c", "d", "e"]);
32 | ```
33 |
34 |
35 | ## Changelog
36 |
37 | See [Releases page](https://github.com/azu/immutable-array-prototype/releases).
38 |
39 | ## Running tests
40 |
41 | Install devDependencies and Run `npm test`:
42 |
43 | npm i -d && npm test
44 |
45 | ## Contributing
46 |
47 | Pull requests and stars are always welcome.
48 |
49 | For bugs and feature requests, [please create an issue](https://github.com/azu/immutable-array-prototype/issues).
50 |
51 | 1. Fork it!
52 | 2. Create your feature branch: `git checkout -b my-new-feature`
53 | 3. Commit your changes: `git commit -am 'Add some feature'`
54 | 4. Push to the branch: `git push origin my-new-feature`
55 | 5. Submit a pull request :D
56 |
57 | ## Author
58 |
59 | - [github/azu](https://github.com/azu)
60 | - [twitter/azu_re](https://twitter.com/azu_re)
61 |
62 | ## License
63 |
64 | MIT © azu
65 |
--------------------------------------------------------------------------------
/packages/unshift/jest.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | "transform": {
3 | ".(ts|tsx)": "ts-jest"
4 | },
5 | "testRegex": "(./test/.*-(test|spec))\\.(ts|tsx|js)$",
6 | "testPathIgnorePatterns": ["/lib/", "/out/", "/node_modules/"],
7 | "moduleFileExtensions": [
8 | "ts",
9 | "tsx",
10 | "js"
11 | ]
12 | };
13 |
--------------------------------------------------------------------------------
/packages/unshift/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "directories": {
3 | "lib": "lib",
4 | "test": "test"
5 | },
6 | "author": "azu",
7 | "license": "MIT",
8 | "files": [
9 | "bin/",
10 | "lib/",
11 | "src/"
12 | ],
13 | "name": "@immutable-array/unshift",
14 | "version": "1.0.6",
15 | "description": "Immutable Array.prototype.unshift.",
16 | "main": "lib/unshift.js",
17 | "types": "lib/unshift.d.ts",
18 | "scripts": {
19 | "build": "tsc -p .",
20 | "watch": "tsc -p . --watch",
21 | "prepublish": "npm run --if-present build",
22 | "test": "jest"
23 | },
24 | "publishConfig": {
25 | "access": "public"
26 | },
27 | "keywords": [
28 | "array",
29 | "immutable"
30 | ],
31 | "repository": {
32 | "type": "git",
33 | "url": "https://github.com/azu/immutable-array-prototype.git"
34 | },
35 | "bugs": {
36 | "url": "https://github.com/azu/immutable-array-prototype/issues"
37 | },
38 | "homepage": "https://github.com/azu/immutable-array-prototype/tree/master/packages/unshift/",
39 | "devDependencies": {
40 | "@types/jest": "^29.2.5",
41 | "@types/node": "^18.11.18",
42 | "jest": "^29.3.1",
43 | "ts-jest": "^29.0.3",
44 | "typescript": "^4.9.4"
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/packages/unshift/src/unshift.ts:
--------------------------------------------------------------------------------
1 | /**
2 | * Inserts new elements at the start of an array.
3 | * @param array base Array.
4 | * @param items Elements to insert at the start of the Array.
5 | */
6 | export function unshift(array: T[], ...items: T[]): T[] {
7 | return items.concat(array);
8 | }
9 |
--------------------------------------------------------------------------------
/packages/unshift/test/unshift-test.ts:
--------------------------------------------------------------------------------
1 | // MIT © 2017 azu
2 | import { unshift } from "../src/unshift";
3 | import * as assert from "assert";
4 |
5 | describe("unshift", () => {
6 | it("can unshift element to empty array", () => {
7 | const originalArray: string[] = [];
8 | const resultArray = unshift(originalArray, "a");
9 | assert.deepStrictEqual(resultArray, ["a"]);
10 | });
11 | it("can unshift multiple elements", () => {
12 | const originalArray = ["a", "b", "c", "d", "e"];
13 | const addingArray = ["f", "g"];
14 | const resultArray = unshift(originalArray, ...addingArray);
15 | assert.deepStrictEqual(resultArray, ["f", "g", "a", "b", "c", "d", "e"]);
16 | });
17 | it("can unshift an array", () => {
18 | const originalArray = ["a", "b", "c", "d", "e"];
19 | const shiftingArray = ["f", "g"];
20 | const resultArray = unshift(originalArray, shiftingArray);
21 | assert.deepStrictEqual(resultArray, [["f", "g"], "a", "b", "c", "d", "e"]);
22 | });
23 | it("should be idempotent - x() === x()", () => {
24 | const originalArray: any[] = ["a", "b", "c", "d", "e"];
25 | assert.deepStrictEqual(unshift(originalArray, 1), unshift(originalArray, 1));
26 | });
27 | it("should return empty array when adding empty element to empty array", () => {
28 | const originalArray: any[] = [];
29 | const resultArray = unshift(originalArray);
30 | assert.strictEqual(resultArray.length, 0);
31 | });
32 | it("should return immutable result array", () => {
33 | const originalArray = ["a", "b", "c", "d", "e"];
34 | const resultArray = unshift(originalArray, "f", "g");
35 | assert.ok(originalArray !== resultArray);
36 | });
37 | it("should return result array have length = original + adding", () => {
38 | const originalArray = ["a", "b", "c", "d", "e"];
39 | const addingArray = ["f", "g"];
40 | const resultArray = unshift(originalArray, ...addingArray);
41 | assert.strictEqual(resultArray.length, originalArray.length + addingArray.length);
42 | });
43 | });
44 |
--------------------------------------------------------------------------------
/packages/unshift/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "extends": "../../tsconfig.json",
3 | "compilerOptions": {
4 | "outDir": "./lib/"
5 | },
6 | "include": [
7 | "src/**/*"
8 | ]
9 | }
10 |
--------------------------------------------------------------------------------
/prettier.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | printWidth: 120,
3 | tabWidth: 4
4 | };
5 |
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | /* Basic Options */
4 | "module": "commonjs",
5 | "moduleResolution": "node",
6 | "newLine": "LF",
7 | "outDir": "./lib/",
8 | "target": "es5",
9 | "sourceMap": true,
10 | "declaration": true,
11 | "jsx": "preserve",
12 | // Transform jsx syntax in babels phase
13 | "lib": [
14 | "es2017",
15 | "dom"
16 | ],
17 | /* Strict Type-Checking Options */
18 | "strict": true,
19 | /* Additional Checks */
20 | "noUnusedLocals": true,
21 | /* Report errors on unused locals. */
22 | "noUnusedParameters": true,
23 | /* Report errors on unused parameters. */
24 | "noImplicitReturns": true,
25 | /* Report error when not all code paths in function return a value. */
26 | "noFallthroughCasesInSwitch": true
27 | /* Report errors for fallthrough cases in switch statement. */
28 | }
29 | }
30 |
--------------------------------------------------------------------------------