Skip to content

Commit 17572b3

Browse files
✨ feat: Add fill, fillfn, and iota.
1 parent 86d2225 commit 17572b3

File tree

13 files changed

+9713
-25
lines changed

13 files changed

+9713
-25
lines changed

README.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
:icecream: [@array-like/fill](https://array-like.github.io/fill)
22
==
33

4-
ArrayLike filling functions for JavaScript.
4+
`ArrayLike` filling functions for JavaScript.
55
See [docs](https://array-like.github.io/fill/index.html).
66

7-
> :building_construction: Caveat emptor! This is work in progress. Code may be
8-
> working. Documentation may be present. Coherence may be. Maybe.
7+
```js
8+
import {alloc} from '@array-like/alloc';
9+
import {iota} from '@array-like/fill';
910

10-
> :warning: Depending on your environment, the code may require
11-
> `regeneratorRuntime` to be defined, for instance by importing
12-
> [regenerator-runtime/runtime](https://www.npmjs.com/package/regenerator-runtime).
11+
const a = alloc(100); // undefined undefined undefined ...
12+
iota(a, 0, 100, 0);
13+
a; // 0 1 2 3 4 ...
14+
15+
import {fill} from '@array-like/fill';
16+
fill(a, 0, 100, -1);
17+
a; // -1 -1 -1 -1 -1 ...
18+
19+
import {fillfn} from '@array-like/fill';
20+
fillfn(a, 0, 100, () => []);
21+
a; // [] [] [] [] [] ...
22+
a[0] !== a[1]; // true
23+
```
1324

1425
[![License](https://img.shields.io/github/license/array-like/fill.svg)](https://raw.githubusercontent.com/array-like/fill/main/LICENSE)
1526
[![Version](https://img.shields.io/npm/v/@array-like/fill.svg)](https://www.npmjs.org/package/@array-like/fill)

doc/manual/usage.md

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
# Usage
22

3-
> :warning: Depending on your environment, the code may require
4-
> `regeneratorRuntime` to be defined, for instance by importing
5-
> [regenerator-runtime/runtime](https://www.npmjs.com/package/regenerator-runtime).
6-
7-
First, require the polyfill at the entry point of your application
8-
```js
9-
await import('regenerator-runtime/runtime.js');
10-
// or
11-
import 'regenerator-runtime/runtime.js';
12-
```
13-
14-
Then, import the library where needed
3+
Import the library where needed
154
```js
165
const fill = await import('@array-like/fill');
176
// or

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
},
6464
"dependencies": {},
6565
"devDependencies": {
66+
"@array-like/alloc": "^0.0.1",
6667
"@babel/core": "7.14.8",
6768
"@babel/preset-env": "7.14.8",
6869
"@babel/register": "7.14.5",

src/fill.js

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

src/fillfn.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Fillfn.
3+
*
4+
* @param {{[x: number]: any}} a
5+
* @param {number} i
6+
* @param {number} j
7+
* @param {Function} fn
8+
*/
9+
const fillfn = (a, i, j, fn) => {
10+
for (; i < j; ++i) a[i] = fn();
11+
};
12+
13+
export default fillfn;

src/index.js

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

src/iota.js

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

test/src/_fixtures.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export const arrayTypes = [
2+
Array,
3+
Int8Array,
4+
Int16Array,
5+
Int32Array,
6+
Uint8Array,
7+
Uint16Array,
8+
Uint32Array,
9+
Uint8ClampedArray,
10+
Float32Array,
11+
Float64Array,
12+
];

test/src/api.js

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

test/src/fill.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import test from 'ava';
2+
import {_calloc} from '@array-like/alloc';
3+
4+
import {arrayTypes} from './_fixtures.js';
5+
6+
import {fill} from '../../src/index.js';
7+
8+
const macro = (t, type) => {
9+
const calloc = _calloc(type);
10+
11+
const n = 100;
12+
const a = calloc(n);
13+
14+
const reseta = () => {
15+
for (let i = 0; i < n; ++i) {
16+
a[i] = 0;
17+
}
18+
};
19+
20+
const check = (i, v) => {
21+
t.deepEqual(a[i], v, `a[${i}] === ${v}`);
22+
};
23+
24+
reseta();
25+
26+
fill(a, 2, 33, 2);
27+
fill(a, 16, 87, 3);
28+
fill(a, 47, 75, 5);
29+
fill(a, 60, 100, 7);
30+
fill(a, 80, 81, 11);
31+
32+
for (let i = 0; i < n; ++i) {
33+
if (i === 80) {
34+
check(i, 11);
35+
} else if (i >= 60) {
36+
check(i, 7);
37+
} else if (i >= 47) {
38+
check(i, 5);
39+
} else if (i >= 16) {
40+
check(i, 3);
41+
} else if (i >= 2) {
42+
check(i, 2);
43+
} else {
44+
check(i, 0);
45+
}
46+
}
47+
};
48+
49+
macro.title = (title, type) => title ?? type.name;
50+
51+
for (const type of arrayTypes) test(macro, type);

0 commit comments

Comments
 (0)