Skip to content

Commit

Permalink
Initial implementation, tests, readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jan 31, 2024
1 parent 18cde47 commit 2084e66
Show file tree
Hide file tree
Showing 17 changed files with 377 additions and 5 deletions.
20 changes: 20 additions & 0 deletions .editorconfig
@@ -0,0 +1,20 @@
root = true

[*]
indent_style = tab
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
max_line_length = 150

[CHANGELOG.md]
indent_style = space
indent_size = 2

[*.json]
max_line_length = off

[Makefile]
max_line_length = off
17 changes: 17 additions & 0 deletions .eslintrc
@@ -0,0 +1,17 @@
{
"root": true,

"extends": "@ljharb",

"globals": {
"DataView": false
},

"rules": {
"new-cap": ["error", {
"capIsNewExceptions": [
"GetIntrinsic",
],
}],
},
}
12 changes: 12 additions & 0 deletions .github/FUNDING.yml
@@ -0,0 +1,12 @@
# These are supported funding model platforms

github: [ljharb]
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: npm/is-typed-array
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
18 changes: 18 additions & 0 deletions .github/workflows/node-aught.yml
@@ -0,0 +1,18 @@
name: 'Tests: node.js < 10'

on: [pull_request, push]

jobs:
tests:
uses: ljharb/actions/.github/workflows/node.yml@main
with:
range: '< 10'
type: minors
command: npm run tests-only

node:
name: 'node < 10'
needs: [tests]
runs-on: ubuntu-latest
steps:
- run: 'echo tests completed'
19 changes: 19 additions & 0 deletions .github/workflows/node-harmony.yml
@@ -0,0 +1,19 @@
name: 'Tests: node.js (harmony)'

on: [pull_request, push]

jobs:
tests:
uses: ljharb/actions/.github/workflows/node.yml@main
with:
range: '>=0.4 <16'
type: minors
command: npm run test:harmony
skip-ls-check: true

node:
name: 'node: harmony'
needs: [tests]
runs-on: ubuntu-latest
steps:
- run: 'echo tests completed'
7 changes: 7 additions & 0 deletions .github/workflows/node-pretest.yml
@@ -0,0 +1,7 @@
name: 'Tests: pretest/posttest'

on: [pull_request, push]

jobs:
tests:
uses: ljharb/actions/.github/workflows/pretest.yml@main
18 changes: 18 additions & 0 deletions .github/workflows/node-tens.yml
@@ -0,0 +1,18 @@
name: 'Tests: node.js >= 10'

on: [pull_request, push]

jobs:
tests:
uses: ljharb/actions/.github/workflows/node.yml@main
with:
range: '>= 10'
type: minors
command: npm run tests-only

node:
name: 'node >= 10'
needs: [tests]
runs-on: ubuntu-latest
steps:
- run: 'echo tests completed'
9 changes: 9 additions & 0 deletions .github/workflows/rebase.yml
@@ -0,0 +1,9 @@
name: Automatic Rebase

on: [pull_request_target]

jobs:
_:
uses: ljharb/actions/.github/workflows/rebase.yml@main
secrets:
token: ${{ secrets.GITHUB_TOKEN }}
12 changes: 12 additions & 0 deletions .github/workflows/require-allow-edits.yml
@@ -0,0 +1,12 @@
name: Require “Allow Edits”

on: [pull_request_target]

jobs:
_:
name: "Require “Allow Edits”"

runs-on: ubuntu-latest

steps:
- uses: ljharb/require-allow-edits@main
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -133,3 +133,5 @@ dist
npm-shrinkwrap.json
package-lock.json
yarn.lock

.npmignore
2 changes: 2 additions & 0 deletions .npmrc
@@ -1 +1,3 @@
package-lock=false
allow-same-version=true
message=v%s
9 changes: 9 additions & 0 deletions .nycrc
@@ -0,0 +1,9 @@
{
"all": true,
"check-coverage": false,
"reporter": ["text-summary", "text", "html", "json"],
"exclude": [
"coverage",
"test"
]
}
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,6 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
71 changes: 69 additions & 2 deletions README.md
@@ -1,2 +1,69 @@
# is-data-view
Is this value a JS DataView? This module works cross-realm/iframe, does not depend on instanceof or mutable properties, and despite ES6 Symbol.toStringTag.
# is-data-view <sup>[![Version Badge][npm-version-svg]][package-url]</sup>

[![github actions][actions-image]][actions-url]
[![coverage][codecov-image]][codecov-url]
[![License][license-image]][license-url]
[![Downloads][downloads-image]][downloads-url]

[![npm badge][npm-badge-png]][package-url]

Is this value a JS DataView? This module works cross-realm/iframe, does not depend on `instanceof` or mutable properties, and despite ES6 Symbol.toStringTag.

## Example

```js
var isDataView = require('is-data-view');
var assert = require('assert');

assert.equal(false, isDataView(undefined));
assert.equal(false, isDataView(null));
assert.equal(false, isDataView(false));
assert.equal(false, isDataView(true));
assert.equal(false, isDataView([]));
assert.equal(false, isDataView({}));
assert.equal(false, isDataView(/a/g));
assert.equal(false, isDataView(new RegExp('a', 'g')));
assert.equal(false, isDataView(new Date()));
assert.equal(false, isDataView(42));
assert.equal(false, isDataView(NaN));
assert.equal(false, isDataView(Infinity));
assert.equal(false, isDataView(new Number(42)));
assert.equal(false, isDataView('foo'));
assert.equal(false, isDataView(Object('foo')));
assert.equal(false, isDataView(function () {}));
assert.equal(false, isDataView(function* () {}));
assert.equal(false, isDataView(x => x * x));
assert.equal(false, isDataView([]));
assert.equal(false, isDataView(new Int8Array()));
assert.equal(false, isDataView(new Uint8Array()));
assert.equal(false, isDataView(new Uint8ClampedArray()));
assert.equal(false, isDataView(new Int16Array()));
assert.equal(false, isDataView(new Uint16Array()));
assert.equal(false, isDataView(new Int32Array()));
assert.equal(false, isDataView(new Uint32Array()));
assert.equal(false, isDataView(new Float32Array()));
assert.equal(false, isDataView(new Float64Array()));
assert.equal(false, isDataView(new BigInt64Array()));
assert.equal(false, isDataView(new BigUint64Array()));

assert.ok(isDataView(new DataView(new ArrayBuffer(0))));
```

## Tests
Simply clone the repo, `npm install`, and run `npm test`

[package-url]: https://npmjs.org/package/is-data-view
[npm-version-svg]: https://versionbadg.es/inspect-js/is-data-view.svg
[deps-svg]: https://david-dm.org/inspect-js/is-data-view.svg
[deps-url]: https://david-dm.org/inspect-js/is-data-view
[dev-deps-svg]: https://david-dm.org/inspect-js/is-data-view/dev-status.svg
[dev-deps-url]: https://david-dm.org/inspect-js/is-data-view#info=devDependencies
[npm-badge-png]: https://nodei.co/npm/is-data-view.png?downloads=true&stars=true
[license-image]: https://img.shields.io/npm/l/is-data-view.svg
[license-url]: LICENSE
[downloads-image]: https://img.shields.io/npm/dm/is-data-view.svg
[downloads-url]: https://npm-stat.com/charts.html?package=is-data-view
[codecov-image]: https://codecov.io/gh/inspect-js/is-data-view/branch/main/graphs/badge.svg
[codecov-url]: https://app.codecov.io/gh/inspect-js/is-data-view/
[actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/inspect-js/is-data-view
[actions-url]: https://github.com/inspect-js/is-data-view/actions
45 changes: 45 additions & 0 deletions index.js
@@ -0,0 +1,45 @@
'use strict';

var GetIntrinsic = require('get-intrinsic');

var $ArrayBuffer = GetIntrinsic('%ArrayBuffer%');
var $DataView = GetIntrinsic('%DataView%', true);

var callBound = require('call-bind/callBound');

var $dataViewBuffer = callBound('DataView.prototype.buffer', true);

var isTypedArray = require('is-typed-array');

// node <= 0.10, < 0.11.4 has a nonconfigurable own property instead of a prototype getter
module.exports = function isDataView(x) {
if (!x || typeof x !== 'object' || !$DataView || isTypedArray(x)) {
return false;
}

if ($dataViewBuffer) {
try {
$dataViewBuffer(x);
return true;
} catch (e) {
return false;
}
}

/*
* try {
* new $DataView(x); // eslint-disable-line no-new
* x is an ArrayBuffer
* } catch (e) {
*/
if (
('getInt8' in x)
&& typeof x.getInt8 === 'function'
&& x.getInt8 === new $DataView(new $ArrayBuffer(1)).getInt8
) {
return true;
}
// }

return false;
};
60 changes: 57 additions & 3 deletions package.json
@@ -1,14 +1,27 @@
{
"name": "is-data-view",
"version": "0.0.0",
"description": "Is this value a JS DataView? This module works cross-realm/iframe, does not depend on instanceof or mutable properties, and despite ES6 Symbol.toStringTag.",
"description": "Is this value a JS DataView? This module works cross-realm/iframe, does not depend on `instanceof` or mutable properties, and despite ES6 Symbol.toStringTag.",
"main": "index.js",
"exports": {
".": "./index.js",
"./package.json": "./package.json"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"prepack": "npmignore --auto --commentLines=autogenerated",
"prepublishOnly": "safe-publish-latest",
"prepublish": "not-in-publish || npm run prepublishOnly",
"prelint": "evalmd README.md",
"lint": "eslint --ext=js,mjs .",
"pretest": "npm run lint",
"tests-only": "nyc tape 'test/**/*.js'",
"test": "npm run tests-only",
"posttest": "aud --production",
"version": "auto-changelog && git add CHANGELOG.md",
"postversion": "auto-changelog && git add CHANGELOG.md && git commit --no-edit --amend && git tag -f \"v$(node -e \"console.log(require('./package.json').version)\")\""
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
},
"repository": {
"type": "git",
Expand All @@ -28,5 +41,46 @@
"bugs": {
"url": "https://github.com/inspect-js/is-data-view/issues"
},
"homepage": "https://github.com/inspect-js/is-data-view#readme"
"homepage": "https://github.com/inspect-js/is-data-view#readme",
"dependencies": {
"is-typed-array": "^1.1.12"
},
"devDependencies": {
"@ljharb/eslint-config": "^21.1.0",
"aud": "^2.0.3",
"auto-changelog": "^2.4.0",
"available-typed-arrays": "^1.0.5",
"es-value-fixtures": "^1.4.2",
"eslint": "=8.8.0",
"evalmd": "^0.0.19",
"for-each": "^0.3.3",
"has-tostringtag": "^1.0.0",
"in-publish": "^2.0.1",
"make-arrow-function": "^1.2.0",
"make-generator-function": "^2.0.0",
"npmignore": "^0.3.0",
"nyc": "^10.3.2",
"object-inspect": "^1.12.3",
"safe-publish-latest": "^2.0.0",
"tape": "^5.6.5"
},
"testling": {
"files": "test/index.js"
},
"engines": {
"node": ">= 0.4"
},
"auto-changelog": {
"output": "CHANGELOG.md",
"template": "keepachangelog",
"unreleased": false,
"commitLimit": false,
"backfillLimit": false,
"hideCredit": true
},
"publishConfig": {
"ignore": [
".github/workflows"
]
}
}

0 comments on commit 2084e66

Please sign in to comment.