Skip to content

Commit 8f7d13d

Browse files
✨ feat: Add copy function.
1 parent 96fda4b commit 8f7d13d

File tree

7 files changed

+9570
-7
lines changed

7 files changed

+9570
-7
lines changed
File renamed without changes.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"@babel/register": "7.14.5",
6767
"@commitlint/cli": "13.1.0",
6868
"@js-library/commitlint-config": "0.0.4",
69+
"@randomized/random": "^4.0.0",
6970
"ava": "3.15.0",
7071
"babel-plugin-transform-remove-console": "6.9.4",
7172
"babel-plugin-unassert": "3.1.0",

src/copy.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Copy.
3+
*
4+
* @param {ArrayLike} a
5+
* @param {number} ai
6+
* @param {number} aj
7+
* @param {{[x: number]: any}} b
8+
* @param {number} bi
9+
*/
10+
const copy = (a, ai, aj, b, bi) => {
11+
for (; ai < aj; ++ai, ++bi) {
12+
b[bi] = a[ai];
13+
}
14+
};
15+
16+
export default copy;

src/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
const answer = 42;
2-
export default answer;
1+
export {default as copy} from './copy.js';

test/src/api.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/src/copy.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import test from 'ava';
2+
3+
import {randint} from '@randomized/random';
4+
5+
import {copy} from '../../src/index.js';
6+
7+
function one(Constructor) {
8+
test(`copy (${Constructor})`, (t) => {
9+
const n = 100;
10+
const ai = 23;
11+
const aj = 73;
12+
const bi = 49;
13+
14+
const a = new Constructor(n);
15+
const b = new Constructor(n);
16+
17+
const resetb = () => {
18+
for (let i = 0; i < n; ++i) {
19+
b[i] = 0;
20+
}
21+
};
22+
23+
for (let i = 0; i < n; ++i) {
24+
a[i] = randint(1, 2 ** 31);
25+
}
26+
27+
resetb();
28+
29+
for (let i = 0; i < n; ++i) {
30+
t.is(b[i], 0, `b[${i}] === 0`);
31+
}
32+
33+
copy(a, 0, n, b, 0);
34+
35+
for (let i = 0; i < n; ++i) {
36+
t.deepEqual(b[i], a[i], `b[${i}] === a[${i}]`);
37+
}
38+
39+
resetb();
40+
41+
copy(a, ai, aj, b, bi);
42+
43+
for (let i = 0; i < n; ++i) {
44+
if (i < bi || i >= bi + aj - ai) {
45+
t.is(b[i], 0, `b[${i}] === 0`);
46+
} else {
47+
const j = i - bi + ai;
48+
t.deepEqual(b[i], a[j], `b[${i}] === a[${j}]`);
49+
}
50+
}
51+
});
52+
}
53+
54+
const arrays = [
55+
Array,
56+
Int8Array,
57+
Int16Array,
58+
Int32Array,
59+
Uint8Array,
60+
Uint16Array,
61+
Uint32Array,
62+
Uint8ClampedArray,
63+
Float32Array,
64+
Float64Array,
65+
];
66+
67+
for (const Constructor of arrays) {
68+
one(Constructor);
69+
}

0 commit comments

Comments
 (0)