File tree Expand file tree Collapse file tree 13 files changed +9554
-25
lines changed Expand file tree Collapse file tree 13 files changed +9554
-25
lines changed File renamed without changes.
Original file line number Diff line number Diff line change 1
1
:seat : [ @array-like/alloc ] ( https://array-like.github.io/alloc )
2
2
==
3
3
4
- ArrayLike allocation helper functions for JavaScript.
4
+ ` ArrayLike ` allocation helper functions for JavaScript.
5
5
See [ docs] ( https://array-like.github.io/alloc/index.html ) .
6
6
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 , malloc , _calloc } from ' @array-like/alloc ' ;
9
9
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 ) .
10
+ alloc (10 ); // Array
11
+ malloc (10 ); // ArrayBuffer
12
+ const calloc = _calloc (Int32Array );
13
+ calloc (10 ); // Int32Array
14
+ ```
13
15
14
16
[ ![ License] ( https://img.shields.io/github/license/array-like/alloc.svg )] ( https://raw.githubusercontent.com/array-like/alloc/main/LICENSE )
15
17
[ ![ Version] ( https://img.shields.io/npm/v/@array-like/alloc.svg )] ( https://www.npmjs.org/package/@array-like/alloc )
Original file line number Diff line number Diff line change 1
1
# Usage
2
2
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
15
4
``` js
16
5
const alloc = await import (' @array-like/alloc' );
17
6
// or
Original file line number Diff line number Diff line change 199
199
" unicorn"
200
200
],
201
201
"rules" : {
202
+ "unicorn/no-new-array" : " off" ,
202
203
"unicorn/filename-case" : [
203
204
" error" ,
204
205
{
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Method that creates an allocator from an array constructor.
3
+ *
4
+ * @param {any } ArrayConstructor
5
+ * @return {Function }
6
+ */
7
+ const _calloc = ( ArrayConstructor ) => ( n ) => new ArrayConstructor ( n ) ;
8
+ export default _calloc ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Method that allocates an Array.
3
+ *
4
+ * @param {number } n
5
+ * @return {Array }
6
+ */
7
+ const alloc = ( n ) => new Array ( n ) ;
8
+ export default alloc ;
Original file line number Diff line number Diff line change 1
- const answer = 42 ;
2
- export default answer ;
1
+ export { default as _calloc } from './_calloc.js' ;
2
+ export { default as alloc } from './alloc.js' ;
3
+ export { default as malloc } from './malloc.js' ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Method that allocates an ArrayBuffer.
3
+ *
4
+ * @param {number } n
5
+ * @return {ArrayBuffer }
6
+ */
7
+ const malloc = ( n ) => new ArrayBuffer ( n ) ;
8
+ export default malloc ;
Original file line number Diff line number Diff line change
1
+ import test from 'ava' ;
2
+
3
+ import { alloc } from '../../src/index.js' ;
4
+
5
+ test ( 'alloc' , ( t ) => {
6
+ const a = alloc ( 10 ) ;
7
+
8
+ t . is ( a . length , 10 , 'check length' ) ;
9
+ t . true ( Array . isArray ( a ) , 'check type' ) ;
10
+ } ) ;
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ import test from 'ava' ;
2
+
3
+ import { _calloc } from '../../src/index.js' ;
4
+
5
+ const macro = ( t , TypedArray , n ) => {
6
+ const calloc = _calloc ( TypedArray ) ;
7
+
8
+ const a = calloc ( n ) ;
9
+
10
+ t . is ( a . length , n , 'check length' ) ;
11
+ t . true ( a instanceof TypedArray , 'check type' ) ;
12
+ } ;
13
+
14
+ macro . title = ( title , TypedArray , n ) =>
15
+ title ?? `_calloc(${ TypedArray . name } )(${ n } )` ;
16
+
17
+ test ( macro , Uint8Array , 10 ) ;
18
+ test ( macro , Uint16Array , 133 ) ;
19
+ test ( macro , Int32Array , 27 ) ;
Original file line number Diff line number Diff line change
1
+ import test from 'ava' ;
2
+
3
+ import { malloc } from '../../src/index.js' ;
4
+
5
+ test ( 'malloc' , ( t ) => {
6
+ const a = malloc ( 10 ) ;
7
+
8
+ t . is ( a . byteLength , 10 , 'check length' ) ;
9
+ t . true ( a instanceof ArrayBuffer , 'check type' ) ;
10
+ } ) ;
You can’t perform that action at this time.
0 commit comments