Skip to content

Commit 593a2c7

Browse files
✨ feat: Add realloc.
1 parent afe9541 commit 593a2c7

File tree

8 files changed

+9555
-27
lines changed

8 files changed

+9555
-27
lines changed

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
ArrayLike reallocation for JavaScript.
55
See [docs](https://array-like.github.io/realloc/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';
10+
import {realloc} from '@array-like/realloc':
911

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).
12+
const a = alloc(100);
13+
iota(a, 0, 100, 0);
14+
const b = realloc(a, 200); // 0 1 2 .. 98 99 undefined undefined ...
15+
```
1316

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

doc/manual/usage.md

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
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
16-
const realloc = await import('@array-like/realloc');
5+
const {realloc} = await import('@array-like/realloc');
176
// or
18-
import * as realloc from '@array-like/realloc';
7+
import {realloc} from '@array-like/realloc';
198
```

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,12 @@
5959
"release": "np --message ':hatching_chick: release: Bumping to v%s.'",
6060
"test": "ava"
6161
},
62-
"dependencies": {},
62+
"dependencies": {
63+
"@array-like/alloc": "^0.0.1",
64+
"@array-like/copy": "^0.0.1"
65+
},
6366
"devDependencies": {
67+
"@array-like/fill": "^0.0.1",
6468
"@babel/core": "7.14.8",
6569
"@babel/preset-env": "7.14.8",
6670
"@babel/register": "7.14.5",

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 realloc} from './realloc.js';

src/realloc.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {alloc} from '@array-like/alloc';
2+
import {copy} from '@array-like/copy';
3+
4+
/**
5+
* Realloc.
6+
*
7+
* @param {ArrayLike} data
8+
* @param {number} length
9+
* @return {Array}
10+
*/
11+
const realloc = (data, length) => {
12+
const pt = alloc(length);
13+
14+
copy(data, 0, Math.min(data.length, length), pt, 0);
15+
16+
return pt;
17+
};
18+
19+
export default realloc;

test/src/api.js

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

test/src/realloc.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import test from 'ava';
2+
import {alloc} from '@array-like/alloc';
3+
import {iota} from '@array-like/fill';
4+
5+
import {realloc} from '../../src/index.js';
6+
7+
test('realloc', (t) => {
8+
const m = 1000;
9+
const n = 733;
10+
11+
const a = alloc(m);
12+
let b = alloc(n);
13+
14+
iota(a, 0, m, 0);
15+
iota(b, 0, n, 0);
16+
17+
b = realloc(b, m);
18+
iota(b, n, m, n);
19+
20+
t.deepEqual(b, a, 'b is a after growing');
21+
22+
a.splice(n, m - n);
23+
b = realloc(b, n);
24+
25+
t.deepEqual(b, a, 'b is a after shrinking');
26+
});

0 commit comments

Comments
 (0)