Skip to content

Commit 8ef0192

Browse files
authored
Merge pull request #52 from aplbrain/modernize-repo
Modernize repo; new node, tsup, vitest
2 parents b0cd99b + 79bbbfd commit 8ef0192

21 files changed

Lines changed: 3154 additions & 2137 deletions

.eslintrc.js

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

.github/workflows/npm-publish.yml

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,43 @@
1-
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2-
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages
3-
4-
name: Node.js Package
1+
name: Publish to npm
52

63
on:
74
release:
85
types: [created]
96

107
jobs:
118
build:
9+
name: Build & Test (matrix)
1210
runs-on: ubuntu-latest
11+
strategy:
12+
matrix:
13+
node: [18, 20, 22]
1314
steps:
14-
- uses: actions/checkout@v2
15-
- uses: actions/setup-node@v2
15+
- uses: actions/checkout@v4
16+
- uses: actions/setup-node@v4
1617
with:
17-
node-version: 12
18-
- run: npm i
18+
node-version: ${{ matrix.node }}
19+
cache: npm
1920
- run: npm ci
21+
- run: npm run typecheck
22+
- run: npm run build
2023
- run: npm test
2124

2225
publish-npm:
23-
needs: build
24-
runs-on: ubuntu-latest
25-
steps:
26-
- uses: actions/checkout@v2
27-
- uses: actions/setup-node@v2
28-
with:
29-
node-version: 12
30-
registry-url: https://registry.npmjs.org/
31-
- run: npm install && npm ci
32-
- run: npm publish
33-
env:
34-
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
35-
36-
publish-gpr:
26+
name: Publish (npm)
3727
needs: build
3828
runs-on: ubuntu-latest
3929
permissions:
30+
id-token: write # for provenance
4031
contents: read
41-
packages: write
4232
steps:
43-
- uses: actions/checkout@v2
44-
- uses: actions/setup-node@v2
33+
- uses: actions/checkout@v4
34+
- uses: actions/setup-node@v4
4535
with:
46-
node-version: 12
47-
registry-url: https://npm.pkg.github.com/
48-
- run: npm install && npm ci
49-
- run: npm publish
36+
node-version: 20
37+
registry-url: https://registry.npmjs.org/
38+
cache: npm
39+
- run: npm ci
40+
# prepublishOnly will run (build + test + typecheck) per your package.json
41+
- run: npm publish --provenance --access public
5042
env:
51-
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
43+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.github/workflows/test-node.yml

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,16 @@
1-
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
2-
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3-
4-
name: Node.js CI
5-
6-
on:
7-
push:
8-
branches: [master]
9-
pull_request:
10-
branches: [master]
11-
1+
name: ci
2+
on: [push, pull_request]
123
jobs:
13-
build:
14-
runs-on: ubuntu-latest
15-
16-
strategy:
17-
matrix:
18-
node-version: [12.x, 14.x, 16.x]
19-
20-
steps:
21-
- uses: actions/checkout@v2
22-
- name: Use Node.js ${{ matrix.node-version }}
23-
uses: actions/setup-node@v1
24-
with:
25-
node-version: ${{ matrix.node-version }}
26-
- run: npm i
27-
- run: npm run build --if-present
28-
- run: npm test
4+
test:
5+
runs-on: ubuntu-latest
6+
strategy:
7+
matrix: { node: [18, 20, 22] }
8+
steps:
9+
- uses: actions/checkout@v4
10+
- uses: actions/setup-node@v4
11+
with:
12+
node-version: ${{ matrix.node }}
13+
- run: npm ci
14+
- run: npm run typecheck
15+
- run: npm test
16+
- run: npm run build

README.md

Lines changed: 93 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,142 @@
1-
<h1 align=center>npy.js</h1>
2-
<h6 align=center>Read .npy files directly in JS</h6>
3-
4-
<p align=center>
5-
<a href="https://www.npmjs.com/package/npyjs"><img src="https://img.shields.io/npm/v/npyjs.svg?style=for-the-badge" /></a>
6-
<a href="https://github.com/aplbrain/npyjs"><img src="https://img.shields.io/github/issues/aplbrain/npyjs.svg?style=for-the-badge" /></a>
7-
<a href="https://github.com/aplbrain/npyjs"><img src="https://img.shields.io/github/license/aplbrain/npyjs.svg?style=for-the-badge" /></a>
8-
<img alt="GitHub Workflow Status" src="https://img.shields.io/github/actions/workflow/status/aplbrain/npyjs/test-node.yml?label=Tests&style=for-the-badge">
1+
<h1 align="center">npyjs</h1>
2+
<h6 align="center">Read NumPy <code>.npy</code> files in JavaScript (Node, Browser, Deno)</h6>
3+
4+
<p align="center">
5+
<a href="https://www.npmjs.com/package/npyjs"><img src="https://img.shields.io/npm/v/npyjs.svg?style=for-the-badge" /></a>
6+
<a href="https://github.com/aplbrain/npyjs/issues"><img src="https://img.shields.io/github/issues/aplbrain/npyjs.svg?style=for-the-badge" /></a>
7+
<a href="https://github.com/aplbrain/npyjs/blob/main/LICENSE"><img src="https://img.shields.io/github/license/aplbrain/npyjs.svg?style=for-the-badge" /></a>
8+
<img alt="GitHub Workflow Status" src="https://img.shields.io/github/actions/workflow/status/aplbrain/npyjs/ci.yml?label=Tests&style=for-the-badge">
99
</p>
1010

11-
Read .npy files from [numpy](https://numpy.org/doc/1.18/reference/generated/numpy.save.html) in Node/JS.
11+
Read `.npy` arrays saved with [NumPy](https://numpy.org/doc/stable/reference/generated/numpy.save.html) directly in modern JavaScript runtimes.
1212

13-
## Installation
13+
---
1414

15-
Include npy.js in your project directly, or:
15+
## Installation
1616

17-
```shell
17+
```bash
18+
npm install npyjs
19+
# or
1820
yarn add npyjs
19-
# npm i npyjs
2021
```
2122

23+
Supports **Node ≥18**, modern browsers, and Deno/Bun.
24+
25+
---
26+
2227
## Import
2328

24-
```javascript
29+
```ts
30+
// Modern named export (recommended)
31+
import { load } from "npyjs";
32+
33+
// Back-compatibility class (matches legacy docs/tests)
2534
import npyjs from "npyjs";
2635
```
2736

37+
---
38+
2839
## Usage
2940

30-
- Create a new npyjs object:
41+
### 1. Functional API (preferred)
3142

32-
```javascript
33-
let n = new npyjs();
34-
// Or with options:
35-
let n = new npyjs({ convertFloat16: false }); // Disable float16 to float32 conversion
43+
```ts
44+
import { load } from "npyjs";
45+
46+
const arr = await load("my-array.npy");
47+
// arr has { data, shape, dtype, fortranOrder }
48+
console.log(arr.shape); // e.g., [100, 784]
3649
```
3750

38-
- This object can now be used load .npy files. Arrays can be returned via a JavaScript callback, so usage looks like this:
51+
### 2. Legacy Class API (still supported)
3952

40-
```javascript
41-
n.load("my-array.npy", (array, shape) => {
42-
// `array` is a one-dimensional array of the raw data
43-
// `shape` is a one-dimensional array that holds a numpy-style shape.
44-
console.log(`You loaded an array with ${array.length} elements and ${shape.length} dimensions.`);
45-
});
46-
```
53+
```ts
54+
import npyjs from "npyjs";
4755

48-
- You can also use this library promise-style using either .then or async await:
56+
// Default options
57+
const n = new npyjs();
58+
59+
// Disable float16→float32 conversion
60+
const n2 = new npyjs({ convertFloat16: false });
4961

50-
```javascript
51-
n.load("test.npy").then((res) => {
52-
// res has { data, shape, dtype } members.
53-
});
62+
const arr = await n.load("my-array.npy");
5463
```
5564

56-
```javascript
57-
const npyArray = await n.load("test.npy");
65+
---
66+
67+
## Accessing multidimensional elements
68+
69+
`npyjs` returns flat typed arrays with a `shape`. `npyjs` also ships a small helper to turn the flat `data` + `shape` into nested JS arrays.
70+
71+
```ts
72+
import { load } from "npyjs";
73+
import { reshape } from "npyjs/reshape";
74+
75+
const { data, shape, fortranOrder } = await load("my-array.npy");
76+
const nested = reshape(data, shape, fortranOrder); // -> arrays nested by dims
5877
```
5978

60-
## Accessing multidimensional array elements
79+
For C-order arrays (the NumPy default), pass fortranOrder = false (default).
80+
81+
For Fortran-order arrays, pass true and the helper will return the natural row-major nested structure.
6182

62-
- You can conveniently access multidimensional array elements using the 'ndarray' library:
83+
Or pair it with [ndarray](https://github.com/scijs/ndarray) or TensorFlow\.js:
6384

64-
```javascript
85+
```ts
6586
import ndarray from "ndarray";
66-
const npyArray = ndarray(data, shape);
67-
npyArray.get(10, 15);
87+
import { load } from "npyjs";
88+
89+
const { data, shape } = await load("my-array.npy");
90+
const tensor = ndarray(data, shape);
91+
92+
console.log(tensor.get(10, 15));
6893
```
6994

70-
## Supported Data Types
95+
---
7196

72-
The library supports the following NumPy data types:
97+
## Supported Data Types
7398

7499
- `int8`, `uint8`
75100
- `int16`, `uint16`
76101
- `int32`, `uint32`
77-
- `int64`, `uint64` (as BigInt)
102+
- `int64`, `uint64` (as `BigInt`)
78103
- `float32`
79104
- `float64`
80105
- `float16` (converted to float32 by default)
81106

82-
### Float16 Support
107+
### Float16 Control
83108

84-
By default, float16 arrays are automatically converted to float32 for compatibility, since JavaScript doesn't natively support float16. You can control this behavior with the constructor options:
85-
86-
```javascript
87-
// Default behavior - float16 is converted to float32
109+
```ts
110+
// Default: converts float16 → float32
88111
const n1 = new npyjs();
89-
// Keep float16 as raw uint16 values without conversion
112+
113+
// Keep raw Uint16Array
90114
const n2 = new npyjs({ convertFloat16: false });
91115
```
92116

93-
Unless otherwise specified, all code inside of this repository is covered under the license in [LICENSE](LICENSE).
117+
---
118+
119+
## Development
120+
121+
- Built with [tsup](https://github.com/egoist/tsup) (dual ESM + CJS + d.ts)
122+
- Tested with [Vitest](https://vitest.dev)
123+
- CI on GitHub Actions (Node 18/20/22)
124+
125+
### Commands
126+
127+
```bash
128+
npm run build # Build to dist/
129+
npm test # Run Vitest
130+
npm run typecheck # TypeScript type checking
131+
```
132+
133+
---
134+
135+
## License
94136

95-
Please report bugs or contribute pull-requests on [GitHub](https://github.com/aplbrain/npyjs).
137+
Apache-2.0 © [JHU APL](http://www.jhuapl.edu/)
96138

97139
---
98140

99141
<p align="center"><small>Made with ♥ at <a href="http://www.jhuapl.edu/"><img alt="JHU APL" align="center" src="./docs/apl-logo.png" height="23px"></a></small></p>
142+
````

0 commit comments

Comments
 (0)