array-string-at
Advanced tools
Comparing version
@@ -5,2 +5,10 @@ # Changelog | ||
## [1.1.0][] - 2021-09-13 | ||
### Added | ||
- TypeScript types | ||
- Test case for `Infinity` | ||
- Support for `TypedArray` | ||
## [1.0.0][] - 2021-06-06 | ||
@@ -14,1 +22,3 @@ | ||
[1.0.0]: https://github.com/niksy/array-string-at/tree/v1.0.0 | ||
[unreleased]: https://github.com/niksy/array-string-at/compare/v1.1.0...HEAD | ||
[1.1.0]: https://github.com/niksy/array-string-at/tree/v1.1.0 |
@@ -5,17 +5,64 @@ 'use strict'; | ||
/* eslint-disable no-undefined */ | ||
var isTypedarray = isTypedArray; | ||
isTypedArray.strict = isStrictTypedArray; | ||
isTypedArray.loose = isLooseTypedArray; | ||
var toString = Object.prototype.toString; | ||
var names = { | ||
'[object Int8Array]': true | ||
, '[object Int16Array]': true | ||
, '[object Int32Array]': true | ||
, '[object Uint8Array]': true | ||
, '[object Uint8ClampedArray]': true | ||
, '[object Uint16Array]': true | ||
, '[object Uint32Array]': true | ||
, '[object Float32Array]': true | ||
, '[object Float64Array]': true | ||
}; | ||
function isTypedArray(arr) { | ||
return ( | ||
isStrictTypedArray(arr) | ||
|| isLooseTypedArray(arr) | ||
) | ||
} | ||
function isStrictTypedArray(arr) { | ||
return ( | ||
arr instanceof Int8Array | ||
|| arr instanceof Int16Array | ||
|| arr instanceof Int32Array | ||
|| arr instanceof Uint8Array | ||
|| arr instanceof Uint8ClampedArray | ||
|| arr instanceof Uint16Array | ||
|| arr instanceof Uint32Array | ||
|| arr instanceof Float32Array | ||
|| arr instanceof Float64Array | ||
) | ||
} | ||
function isLooseTypedArray(arr) { | ||
return names[toString.call(arr)] | ||
} | ||
/* globals unknown */ | ||
/** | ||
* @param {string|Array} item | ||
* @param {number} _index | ||
* @typedef {Int8Array | Int16Array | Int32Array | Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array | Float32Array | Float64Array} TypedArray | ||
*/ | ||
/** | ||
* Returns value at specified index. Supports relative indexing from the end when passed a negative index. Uses native implementation if available. | ||
* | ||
* @returns {*} | ||
* @throws {TypeError} | ||
* @param {string|Array<unknown>|TypedArray} item | ||
* @param {number} [index] | ||
*/ | ||
function ponyfill(item, _index) { | ||
if (typeof item !== 'string' && !Array.isArray(item)) { | ||
function ponyfill(item, index) { | ||
var _index; | ||
if (typeof item !== 'string' && !Array.isArray(item) && !isTypedarray.loose(item)) { | ||
throw new TypeError('Expected a string or array-like item.'); | ||
} | ||
var index = Math.trunc(_index) || 0; | ||
index = Math.trunc((_index = index) !== null && _index !== void 0 ? _index : 0) || 0; | ||
@@ -32,14 +79,18 @@ if (index < 0) { | ||
} | ||
var preferNative = function preferNative(item, index) { | ||
if (typeof item === 'string' && typeof String.prototype.at !== 'undefined' || Array.isArray(item) && Array.prototype.at !== 'undefined') { | ||
return item.at(index); | ||
} | ||
/* istanbul ignore next */ | ||
/** | ||
* Returns value at specified index. Supports relative indexing from the end when passed a negative index. Uses native implementation if available. | ||
* | ||
* @param {string|Array<unknown>|TypedArray} item Item to search. | ||
* @param {number} [index] Relative index. | ||
*/ | ||
return ponyfill(item, index); | ||
}; | ||
function preferNative(item, index) { | ||
var _item$at, _item$at2; | ||
exports.default = ponyfill; | ||
return (_item$at = item === null || item === void 0 ? void 0 : (_item$at2 = item.at) === null || _item$at2 === void 0 ? void 0 : _item$at2.call(item, index !== null && index !== void 0 ? index : 0)) !== null && _item$at !== void 0 ? _item$at : ponyfill(item, index); | ||
} | ||
exports['default'] = ponyfill; | ||
exports.preferNative = preferNative; | ||
//# sourceMappingURL=index.js.map |
@@ -1,16 +0,63 @@ | ||
/* eslint-disable no-undefined */ | ||
var isTypedarray = isTypedArray; | ||
isTypedArray.strict = isStrictTypedArray; | ||
isTypedArray.loose = isLooseTypedArray; | ||
var toString = Object.prototype.toString; | ||
var names = { | ||
'[object Int8Array]': true | ||
, '[object Int16Array]': true | ||
, '[object Int32Array]': true | ||
, '[object Uint8Array]': true | ||
, '[object Uint8ClampedArray]': true | ||
, '[object Uint16Array]': true | ||
, '[object Uint32Array]': true | ||
, '[object Float32Array]': true | ||
, '[object Float64Array]': true | ||
}; | ||
function isTypedArray(arr) { | ||
return ( | ||
isStrictTypedArray(arr) | ||
|| isLooseTypedArray(arr) | ||
) | ||
} | ||
function isStrictTypedArray(arr) { | ||
return ( | ||
arr instanceof Int8Array | ||
|| arr instanceof Int16Array | ||
|| arr instanceof Int32Array | ||
|| arr instanceof Uint8Array | ||
|| arr instanceof Uint8ClampedArray | ||
|| arr instanceof Uint16Array | ||
|| arr instanceof Uint32Array | ||
|| arr instanceof Float32Array | ||
|| arr instanceof Float64Array | ||
) | ||
} | ||
function isLooseTypedArray(arr) { | ||
return names[toString.call(arr)] | ||
} | ||
/* globals unknown */ | ||
/** | ||
* @param {string|Array} item | ||
* @param {number} _index | ||
* @typedef {Int8Array | Int16Array | Int32Array | Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array | Float32Array | Float64Array} TypedArray | ||
*/ | ||
/** | ||
* Returns value at specified index. Supports relative indexing from the end when passed a negative index. Uses native implementation if available. | ||
* | ||
* @returns {*} | ||
* @throws {TypeError} | ||
* @param {string|Array<unknown>|TypedArray} item | ||
* @param {number} [index] | ||
*/ | ||
function ponyfill(item, _index) { | ||
if (typeof item !== 'string' && !Array.isArray(item)) { | ||
function ponyfill(item, index) { | ||
var _index; | ||
if (typeof item !== 'string' && !Array.isArray(item) && !isTypedarray.loose(item)) { | ||
throw new TypeError('Expected a string or array-like item.'); | ||
} | ||
var index = Math.trunc(_index) || 0; | ||
index = Math.trunc((_index = index) !== null && _index !== void 0 ? _index : 0) || 0; | ||
@@ -27,14 +74,17 @@ if (index < 0) { | ||
} | ||
var preferNative = function preferNative(item, index) { | ||
if (typeof item === 'string' && typeof String.prototype.at !== 'undefined' || Array.isArray(item) && Array.prototype.at !== 'undefined') { | ||
return item.at(index); | ||
} | ||
/* istanbul ignore next */ | ||
/** | ||
* Returns value at specified index. Supports relative indexing from the end when passed a negative index. Uses native implementation if available. | ||
* | ||
* @param {string|Array<unknown>|TypedArray} item Item to search. | ||
* @param {number} [index] Relative index. | ||
*/ | ||
return ponyfill(item, index); | ||
}; | ||
function preferNative(item, index) { | ||
var _item$at, _item$at2; | ||
export default ponyfill; | ||
export { preferNative }; | ||
return (_item$at = item === null || item === void 0 ? void 0 : (_item$at2 = item.at) === null || _item$at2 === void 0 ? void 0 : _item$at2.call(item, index !== null && index !== void 0 ? index : 0)) !== null && _item$at !== void 0 ? _item$at : ponyfill(item, index); | ||
} | ||
export { ponyfill as default, preferNative }; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "array-string-at", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Array/string `.at()` ponyfill.", | ||
@@ -17,2 +17,3 @@ "license": "MIT", | ||
"module": "esm/index.js", | ||
"types": "esm/index.d.ts", | ||
"directories": { | ||
@@ -32,3 +33,3 @@ "test": "test" | ||
"module-check": "node -e 'require(\"array-string-at\");' && node --input-type=module -e 'import \"array-string-at\";'", | ||
"prepublishOnly": "npm run build && npm run module-check", | ||
"prepublishOnly": "npm run build && npm run lint:types && npm run module-check", | ||
"postpublish": "GITHUB_TOKEN=$GITHUB_RELEASE_TOKEN github-release-from-changelog", | ||
@@ -39,4 +40,9 @@ "release": "np --no-release-draft", | ||
"test:automated:watch": "npm run test:automated -- --auto-watch --no-single-run", | ||
"version": "if [[ $(git rev-parse --abbrev-ref HEAD) == 'master' ]]; then version-changelog CHANGELOG.md && changelog-verify CHANGELOG.md && git add CHANGELOG.md; else echo; fi" | ||
"version": "if [ $(git rev-parse --abbrev-ref HEAD) == 'master' ]; then version-changelog CHANGELOG.md && changelog-verify CHANGELOG.md && git add CHANGELOG.md; else echo; fi", | ||
"lint:types": "tsc" | ||
}, | ||
"dependencies": { | ||
"@types/is-typedarray": "^1.0.0", | ||
"is-typedarray": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
@@ -51,16 +57,20 @@ "@babel/cli": "^7.2.3", | ||
"@rollup/plugin-node-resolve": "^9.0.0", | ||
"@types/karma-fixture": "^0.2.5", | ||
"@types/mocha": "^8.2.3", | ||
"@types/node": "^16.3.0", | ||
"changelog-verify": "^1.1.2", | ||
"core-js": "^2.6.5", | ||
"eslint": "^7.11.0", | ||
"eslint-config-niksy": "^9.0.0", | ||
"eslint-config-prettier": "^6.14.0", | ||
"eslint-plugin-extend": "^0.1.1", | ||
"eslint-plugin-import": "^2.22.1", | ||
"eslint-plugin-jsdoc": "^30.7.3", | ||
"cpy": "^8.1.2", | ||
"eslint": "^7.31.0", | ||
"eslint-config-niksy": "^10.0.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-import": "^2.23.4", | ||
"eslint-plugin-jsdoc": "^33.3.0", | ||
"eslint-plugin-mocha": "^8.0.0", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-prettier": "^3.0.1", | ||
"eslint-plugin-promise": "^4.1.1", | ||
"eslint-plugin-unicorn": "^23.0.0", | ||
"eslint-plugin-prettier": "^3.4.0", | ||
"eslint-plugin-promise": "^5.1.0", | ||
"eslint-plugin-unicorn": "^31.0.0", | ||
"esm": "^3.0.51", | ||
"execa": "^5.1.1", | ||
"github-release-from-changelog": "^2.1.1", | ||
@@ -71,3 +81,3 @@ "husky": "^4.3.0", | ||
"karma-chrome-launcher": "^3.1.0", | ||
"karma-coverage-istanbul-reporter": "^3.0.3", | ||
"karma-coverage": "^2.0.3", | ||
"karma-fixture": "^0.2.6", | ||
@@ -87,2 +97,3 @@ "karma-html2js-preprocessor": "^1.1.0", | ||
"rollup-plugin-node-globals": "^1.4.0", | ||
"typescript": "^4.3.5", | ||
"version-changelog": "^3.1.1" | ||
@@ -89,0 +100,0 @@ }, |
# array-string-at | ||
[![Build Status][ci-img]][ci] | ||
[![BrowserStack Status][browserstack-img]][browserstack] | ||
[![Browser testing by BrowserStack][browserstack-img]][browserstack] | ||
@@ -62,3 +62,3 @@ [Array/string `.at()`](https://github.com/tc39/proposal-relative-indexing-method) | ||
Type: `string|Array` | ||
Type: `string|Array<unknown>|TypedArray` | ||
@@ -97,4 +97,4 @@ Item to search. | ||
[browserstack]: https://www.browserstack.com/ | ||
[browserstack-img]: https://www.browserstack.com/automate/badge.svg?badge_key=RUJVZTdFUy9NenNXaTdMYU54aDlnKy9TOFNhNTl4ZlBPNitVVXh1ZWpBVT0tLUpFWk9iWkxRdDV5eUJiWVg3Qm1QTnc9PQ==--e7a7b6fb5fd32b1dd45755978da5c71ab1dbaca0 | ||
[browserstack-img]: https://img.shields.io/badge/browser%20testing-BrowserStack-informational?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA2NCA2NCI+CiAgPGRlZnMvPgogIDxyYWRpYWxHcmFkaWVudCBpZD0iYSIgY3g9IjIwLjk0Mjk3NiIgY3k9IjI4LjA5NDY3ODczIiByPSIzLjc5MTM0MTQxIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSI+CiAgICA8c3RvcCBvZmZzZXQ9IjAiIHN0b3AtY29sb3I9IiM3OTc5NzkiLz4KICAgIDxzdG9wIG9mZnNldD0iMSIgc3RvcC1jb2xvcj0iIzRjNGM0YyIvPgogIDwvcmFkaWFsR3JhZGllbnQ+CiAgPGcgdHJhbnNmb3JtPSJ0cmFuc2xhdGUoLTI5LjcyOTIwNCAtNTcuMTg3NjExKSBzY2FsZSgyLjk3MjkyKSI+CiAgICA8Y2lyY2xlIGN4PSIyMC43ODkiIGN5PSIzMC4wMjUiIHI9IjEwLjczOSIgZmlsbD0iI2Y0Yjk2MCIvPgogICAgPGNpcmNsZSBjeD0iMTkuNyIgY3k9IjI4LjkzNiIgcj0iOS43IiBmaWxsPSIjZTY2ZjMyIi8+CiAgICA8Y2lyY2xlIGN4PSIyMS4wMzYiIGN5PSIyNy42OTkiIHI9IjguNDEzIiBmaWxsPSIjZTQzYzQxIi8+CiAgICA8Y2lyY2xlIGN4PSIyMS42NzkiIGN5PSIyOC4zNDIiIHI9IjcuNzIiIGZpbGw9IiNiZGQwNDEiLz4KICAgIDxjaXJjbGUgY3g9IjIxLjEzNSIgY3k9IjI4LjkzNiIgcj0iNy4xNzYiIGZpbGw9IiM2ZGI1NGMiLz4KICAgIDxjaXJjbGUgY3g9IjE5Ljk5NyIgY3k9IjI3Ljc0OCIgcj0iNS45ODgiIGZpbGw9IiNhZWRhZTYiLz4KICAgIDxjaXJjbGUgY3g9IjIwLjkzNyIgY3k9IjI2Ljc1OCIgcj0iNS4wNDgiIGZpbGw9IiM1NmI4ZGUiLz4KICAgIDxjaXJjbGUgY3g9IjIxLjU4IiBjeT0iMjcuNDUxIiByPSI0LjQwNSIgZmlsbD0iIzAwYjFkNSIvPgogICAgPGNpcmNsZSBjeD0iMjAuOTM3IiBjeT0iMjguMDQ1IiByPSIzLjc2MSIgZmlsbD0idXJsKCNhKSIvPgogICAgPGNpcmNsZSBjeD0iMjAuOTM3IiBjeT0iMjguMDQ1IiByPSIzLjc2MSIgZmlsbD0iIzIyMWYxZiIvPgogICAgPGVsbGlwc2UgY3g9Ii0xNS4xNTkiIGN5PSIzMS40MDEiIGZpbGw9IiNmZmYiIHJ4PSIxLjE4OCIgcnk9Ii43NDIiIHRyYW5zZm9ybT0icm90YXRlKC02NS44MzQpIi8+CiAgPC9nPgo8L3N2Zz4K | ||
<!-- prettier-ignore-end --> |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
26584
105.2%14
40%187
192.19%2
Infinity%48
11.63%1
Infinity%+ Added
+ Added
+ Added
+ Added
+ Added
+ Added