Skip to content

Commit 2db6cad

Browse files
committed
Initial implementation, tests, readme
1 parent 70cf325 commit 2db6cad

15 files changed

+236
-3
lines changed

.eslintrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"root": true,
3+
4+
"extends": "@ljharb",
5+
}

.github/FUNDING.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# These are supported funding model platforms
2+
3+
github: [ljharb]
4+
patreon: # Replace with a single Patreon username
5+
open_collective: # Replace with a single Open Collective username
6+
ko_fi: # Replace with a single Ko-fi username
7+
tidelift: npm/array-buffer-byte-length
8+
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9+
liberapay: # Replace with a single Liberapay username
10+
issuehunt: # Replace with a single IssueHunt username
11+
otechie: # Replace with a single Otechie username
12+
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']

.github/workflows/node-aught.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: 'Tests: node.js < 10'
2+
3+
on: [pull_request, push]
4+
5+
jobs:
6+
tests:
7+
uses: ljharb/actions/.github/workflows/node.yml@main
8+
with:
9+
range: '< 10'
10+
type: minors
11+
command: npm run tests-only
12+
13+
node:
14+
name: 'node < 10'
15+
needs: [tests]
16+
runs-on: ubuntu-latest
17+
steps:
18+
- run: 'echo tests completed'

.github/workflows/node-pretest.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name: 'Tests: pretest/posttest'
2+
3+
on: [pull_request, push]
4+
5+
jobs:
6+
tests:
7+
uses: ljharb/actions/.github/workflows/pretest.yml@main

.github/workflows/node-tens.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: 'Tests: node.js >= 10'
2+
3+
on: [pull_request, push]
4+
5+
jobs:
6+
tests:
7+
uses: ljharb/actions/.github/workflows/node.yml@main
8+
with:
9+
range: '>= 10'
10+
type: minors
11+
command: npm run tests-only
12+
13+
node:
14+
name: 'node >= 10'
15+
needs: [tests]
16+
runs-on: ubuntu-latest
17+
steps:
18+
- run: 'echo tests completed'

.github/workflows/rebase.yml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: Automatic Rebase
2+
3+
on: [pull_request_target]
4+
5+
jobs:
6+
_:
7+
uses: ljharb/actions/.github/workflows/rebase.yml@main
8+
secrets:
9+
token: ${{ secrets.GITHUB_TOKEN }}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: Require “Allow Edits”
2+
3+
on: [pull_request_target]
4+
5+
jobs:
6+
_:
7+
name: "Require “Allow Edits”"
8+
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- uses: ljharb/require-allow-edits@main

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,5 @@ dist
107107
npm-shrinkwrap.json
108108
package-lock.json
109109
yarn.lock
110+
111+
.npmignore

.npmrc

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
package-lock=false
2+
allow-same-version=true
3+
message=v%s

.nycrc

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"all": true,
3+
"check-coverage": false,
4+
"reporter": ["text-summary", "text", "html", "json"],
5+
"lines": 86,
6+
"statements": 85.93,
7+
"functions": 82.43,
8+
"branches": 76.06,
9+
"exclude": [
10+
"coverage",
11+
"test"
12+
]
13+
}

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

README.md

+39-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,40 @@
1-
# array-buffer-byte-length
1+
# array-buffer-byte-length <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
2+
3+
[![github actions][actions-image]][actions-url]
4+
[![coverage][codecov-image]][codecov-url]
5+
[![License][license-image]][license-url]
6+
[![Downloads][downloads-image]][downloads-url]
7+
8+
[![npm badge][npm-badge-png]][package-url]
9+
210
Get the byte length of an ArrayBuffer, even in engines without a `.byteLength` method.
11+
12+
## Example
13+
14+
```js
15+
const assert = require('assert');
16+
const byteLength = require('array-buffer-byte-length');
17+
18+
assert.equal(byteLength([]), NaN, 'an array is not an ArrayBuffer, yields NaN');
19+
20+
assert.equal(byteLength(new ArrayBuffer(0)), 0, 'ArrayBuffer of byteLength 0, yields 0');
21+
```
22+
23+
## Tests
24+
Simply clone the repo, `npm install`, and run `npm test`
25+
26+
[package-url]: https://npmjs.org/package/array-buffer-byte-length
27+
[npm-version-svg]: https://versionbadg.es/inspect-js/array-buffer-byte-length.svg
28+
[deps-svg]: https://david-dm.org/inspect-js/array-buffer-byte-length.svg
29+
[deps-url]: https://david-dm.org/inspect-js/array-buffer-byte-length
30+
[dev-deps-svg]: https://david-dm.org/inspect-js/array-buffer-byte-length/dev-status.svg
31+
[dev-deps-url]: https://david-dm.org/inspect-js/array-buffer-byte-length#info=devDependencies
32+
[npm-badge-png]: https://nodei.co/npm/array-buffer-byte-length.png?downloads=true&stars=true
33+
[license-image]: https://img.shields.io/npm/l/array-buffer-byte-length.svg
34+
[license-url]: LICENSE
35+
[downloads-image]: https://img.shields.io/npm/dm/array-buffer-byte-length.svg
36+
[downloads-url]: https://npm-stat.com/charts.html?package=array-buffer-byte-length
37+
[codecov-image]: https://codecov.io/gh/inspect-js/array-buffer-byte-length/branch/main/graphs/badge.svg
38+
[codecov-url]: https://app.codecov.io/gh/inspect-js/array-buffer-byte-length/
39+
[actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/inspect-js/array-buffer-byte-length
40+
[actions-url]: https://github.com/inspect-js/array-buffer-byte-length/actions

index.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
var callBound = require('call-bind/callBound');
4+
var $byteLength = callBound('ArrayBuffer.prototype.byteLength', true);
5+
6+
var isArrayBuffer = require('is-array-buffer');
7+
8+
module.exports = function byteLength(ab) {
9+
if (!isArrayBuffer(ab)) {
10+
return NaN;
11+
}
12+
return $byteLength ? $byteLength(ab) : ab.byteLength;
13+
}; // in node < 0.11, byteLength is an own nonconfigurable property

package.json

+54-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,22 @@
33
"version": "0.0.0",
44
"description": "Get the byte length of an ArrayBuffer, even in engines without a `.byteLength` method.",
55
"main": "index.js",
6+
"exports": {
7+
".": "./index.js",
8+
"./package.json": "./package.json"
9+
},
610
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
11+
"prepack": "npmignore --auto --commentLines=autogenerated",
12+
"prepublishOnly": "safe-publish-latest",
13+
"prepublish": "not-in-publish || npm run prepublishOnly",
14+
"pretest": "npm run lint",
15+
"prelint": "evalmd README.md",
16+
"lint": "eslint --ext=js,mjs .",
17+
"tests-only": "nyc tape 'test/**/*.js'",
18+
"test": "npm run tests-only",
19+
"posttest": "aud --production",
20+
"version": "auto-changelog && git add CHANGELOG.md",
21+
"postversion": "auto-changelog && git add CHANGELOG.md && git commit --no-edit --amend && git tag -f \"v$(node -e \"console.log(require('./package.json').version)\")\""
822
},
923
"keywords": [
1024
"shim",
@@ -17,6 +31,9 @@
1731
"es-shims"
1832
],
1933
"author": "Jordan Harband <ljharb@gmail.com>",
34+
"funding": {
35+
"url": "https://github.com/sponsors/ljharb"
36+
},
2037
"license": "MIT",
2138
"repository": {
2239
"type": "git",
@@ -25,5 +42,40 @@
2542
"bugs": {
2643
"url": "https://github.com/inspect-js/array-buffer-byte-length/issues"
2744
},
28-
"homepage": "https://github.com/inspect-js/array-buffer-byte-length#readme"
45+
"homepage": "https://github.com/inspect-js/array-buffer-byte-length#readme",
46+
"dependencies": {
47+
"call-bind": "^1.0.2",
48+
"is-array-buffer": "^3.0.1"
49+
},
50+
"devDependencies": {
51+
"@ljharb/eslint-config": "^21.0.1",
52+
"aud": "^2.0.2",
53+
"auto-changelog": "^2.4.0",
54+
"es-value-fixtures": "^1.4.2",
55+
"eslint": "=8.8.0",
56+
"evalmd": "^0.0.19",
57+
"for-each": "^0.3.3",
58+
"in-publish": "^2.0.1",
59+
"npmignore": "^0.3.0",
60+
"nyc": "^10.3.2",
61+
"object-inspect": "^1.12.3",
62+
"safe-publish-latest": "^2.0.0",
63+
"tape": "^5.6.3"
64+
},
65+
"auto-changelog": {
66+
"output": "CHANGELOG.md",
67+
"template": "keepachangelog",
68+
"unreleased": false,
69+
"commitLimit": false,
70+
"backfillLimit": false,
71+
"hideCredit": true
72+
},
73+
"testling": {
74+
"files": "test/index.js"
75+
},
76+
"publishConfig": {
77+
"ignore": [
78+
".github/workflows"
79+
]
80+
}
2981
}

test/index.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
var test = require('tape');
4+
var forEach = require('for-each');
5+
var v = require('es-value-fixtures');
6+
var inspect = require('object-inspect');
7+
8+
var byteLength = require('../');
9+
10+
test('byteLength', function (t) {
11+
forEach(v.objects.concat(v.primitives), function (nonAB) {
12+
t.equal(byteLength(nonAB), NaN, inspect(nonAB) + ' is not an ArrayBuffer, and yields NaN');
13+
});
14+
15+
t.test('ArrayBuffers', { skip: typeof ArrayBuffer !== 'function' }, function (st) {
16+
var ab32 = new ArrayBuffer(32);
17+
st.equal(byteLength(ab32), 32, 'works on an ArrayBuffer of length 32: ' + inspect(ab32));
18+
19+
var ab0 = new ArrayBuffer(0);
20+
st.equal(byteLength(ab0), 0, 'works on an ArrayBuffer of length 0: ' + inspect(ab0));
21+
22+
st.end();
23+
});
24+
25+
t.end();
26+
});

0 commit comments

Comments
 (0)