Skip to content

feat(copyWithin): implement immutable Array#copyWithin #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ This library provide immutable version of each methods.

| Native method: Return type | `@immutable-array/*` |
| ---------------------------------------- | ---------------------------------------- |
| [`Array.prototype.copyWithin()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/copyWithin): `Array`| `copyWithin()`: new `Array` |
| [`Array.prototype.fill()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/fill): `Array`| [`fill()`](packages/fill): new `Array` |
| [`Array.prototype.pop()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/pop): `any`| [`pop()`](packages/pop): new `Array` |
| [`Array.prototype.push()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/push): `Number`| [`push()`](packages/push): new `Array` |
| [`Array.prototype.shift()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/shift): `any`| [`shift()`](packages/shift): new `Array` |
| [`Array.prototype.unshift()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift): `any`| [`unshift()`](packages/unshift): new `Array` |
| [`Array.prototype.splice()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/splice): `Array`| [`splice()`](packages/splice): new `Array` |
| [`Array.prototype.reverse()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse): `Array`| [`reverse()`](packages/sort): new `Array` |
| [`Array.prototype.sort()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/sort): `Array`| [`sort()`](packages/sort): new `Array` |
| [`Array.prototype.fill()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/fill): `Array`| [`fill()`](packages/fill): new `Array` |
| [`Array.prototype.copyWithin()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/copyWithin): `Array`| [`copyWithin()`](packages/copy-within): new `Array` |


## Install
Expand Down
19 changes: 19 additions & 0 deletions packages/copy-within/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2017 azu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
82 changes: 82 additions & 0 deletions packages/copy-within/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# @immutable-array/copy-within

Immutable [`Array.prototype.copyWithin()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/copyWithin).

## Install

Install with [npm](https://www.npmjs.com/):

npm install @immutable-array/copy-within

## Usage

Same API with [`Array.prototype.copyWithin()`](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/copyWithin)

```ts
/**
* Returns the this object after copying a section of the array identified by start and end
* to the same array starting at position target
* @param array Base array
* @param target If target is negative, it is treated as length+target where length is the
* length of the array.
* @param start If start is negative, it is treated as length+start. If end is negative, it
* is treated as length+end.
* @param end If not specified, length of the this object is used as its default value.
*/
export declare function copyWithin<T>(array: Array<T>, target: number, start: number, end?: number): Array<T>;
```

## Example

```js
import { copyWithin } from "@immutable-array/copyWithin";
const originalArray = [1, 2, 3, 4, 5];
assert.deepStrictEqual(copyWithin(originalArray, -2), [1, 2, 3, 1, 2]);
assert.deepStrictEqual(copyWithin(originalArray, 0, 3), [4, 5, 3, 4, 5]);
assert.deepStrictEqual(copyWithin(originalArray, 0, 3, 4), [4, 2, 3, 4, 5]);
assert.deepStrictEqual(copyWithin(originalArray, 0, -2, -1), [4, 2, 3, 4, 5]);
```

TODO: Array-like.

This is not specified behavior.

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").

```js
import { copyWithin } from "@immutable-array/copyWithin";
const arrayLike = { length: 5, 3: 1 };
let actual = copyWithin(arrayLike, 0, 3);
assert.deepStrictEqual(actual, [1, , , 1,]);
```

## Changelog

See [Releases page](https://github.com/azu/immutable-array-prototype/releases).

## Running tests

Install devDependencies and Run `npm test`:

npm i -d && npm test

## Contributing

Pull requests and stars are always welcome.

For bugs and feature requests, [please create an issue](https://github.com/azu/immutable-array-prototype/issues).

1. Fork it!
2. Create your feature branch: `git checkout -b my-new-feature`
3. Commit your changes: `git commit -am 'Add some feature'`
4. Push to the branch: `git push origin my-new-feature`
5. Submit a pull request :D

## Author

- [github/azu](https://github.com/azu)
- [twitter/azu_re](https://twitter.com/azu_re)

## License

MIT © azu
39 changes: 39 additions & 0 deletions packages/copy-within/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"directories": {
"lib": "lib",
"test": "test"
},
"author": "azu",
"license": "MIT",
"files": [
"bin/",
"lib/",
"src/"
],
"name": "@immutable-array/copy-within",
"version": "1.0.0",
"description": "Immutable Array.prototype.copyWithin().",
"main": "lib/copy-within.js",
"scripts": {
"build": "cross-env NODE_ENV=production tsc -p .",
"watch": "tsc -p . --watch",
"prepublish": "npm run --if-present build"
},
"keywords": [
"array",
"immutable"
],
"repository": {
"type": "git",
"url": "https://github.com/azu/immutable-array-prototype.git"
},
"bugs": {
"url": "https://github.com/azu/immutable-array-prototype/issues"
},
"homepage": "https://github.com/azu/immutable-array-prototype/tree/master/packages/copy-within/",
"devDependencies": {
"@types/node": "^8.0.2",
"cross-env": "^5.0.1",
"typescript": "^2.3.4"
}
}
13 changes: 13 additions & 0 deletions packages/copy-within/src/copy-within.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Returns the this object after copying a section of the array identified by start and end
* to the same array starting at position target
* @param array Base array
* @param target If target is negative, it is treated as length+target where length is the
* length of the array.
* @param start If start is negative, it is treated as length+start. If end is negative, it
* is treated as length+end.
* @param end If not specified, length of the this object is used as its default value.
*/
export function copyWithin<T>(array: Array<T>, target: number, start: number, end: number = array.length): Array<T> {
return Array.prototype.slice.call(array).copyWithin(target, start, end);
}
35 changes: 35 additions & 0 deletions packages/copy-within/test/copy-within-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// MIT © 2017 azu
import * as assert from "assert";
import { copyWithin } from "../src/copy-within";

describe("copyWithin", () => {
it("should return immutable array", () => {
const originalArray = ["a", "b", "c", "d", "e"];
const resultArray = copyWithin(originalArray, 1);
assert.ok(originalArray !== resultArray);
});

it("can copyWithin empty array", () => {
const originalArray = [];
assert.deepStrictEqual(originalArray.copyWithin(0), []);
});

it("should be idempotent - x() === x()", () => {
const originalArray = ["a", "b", "c", "d", "e"];
assert.deepStrictEqual(originalArray.copyWithin(-2), originalArray.copyWithin(-2));
});

it("should work copyWithin", () => {
// https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/copyWithin
const originalArray = [1, 2, 3, 4, 5];
assert.deepStrictEqual(copyWithin(originalArray, -2), [1, 2, 3, 1, 2]);
assert.deepStrictEqual(copyWithin(originalArray, 0, 3), [4, 5, 3, 4, 5]);
assert.deepStrictEqual(copyWithin(originalArray, 0, 3, 4), [4, 2, 3, 4, 5]);
assert.deepStrictEqual(copyWithin(originalArray, 0, -2, -1), [4, 2, 3, 4, 5]);
});
it("should copyWithin to Array-like", () => {
const arrayLike = { length: 5, 3: 1 };
let actual = copyWithin(arrayLike, 0, 3);
assert.deepStrictEqual(actual, [1, , , 1]);
});
});
9 changes: 9 additions & 0 deletions packages/copy-within/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./lib/"
},
"include": [
"src/**/*"
]
}