3783aa4ae1befee83964531e4145aafea1cc9e119bffe4f3496eb641defbef6b8cd3e61d03297d26129245fb78d6e7b3bc854a50ccfd57a8d5cda591ab23e1 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # TypedArray.prototype.slice <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
  2. [![github actions][actions-image]][actions-url]
  3. [![coverage][codecov-image]][codecov-url]
  4. [![License][license-image]][license-url]
  5. [![Downloads][downloads-image]][downloads-url]
  6. [![npm badge][npm-badge-png]][package-url]
  7. An ES spec-compliant `TypedArray.prototype.slice` shim. Invoke its "shim" method to shim TypedArray.prototype.slice if it is unavailable.
  8. This package implements the [es-shim API](https://github.com/es-shims/api) interface. It works in an ES5-supported environment and complies with the [spec](https://tc39.es/ecma262/#sec-%typedarray%.prototype.slice).
  9. Most common usage:
  10. ```js
  11. var assert = require('assert');
  12. var slice = require('typedarray.prototype.slice');
  13. var arr = new Uint8Array([1, 2, 3]);
  14. var arr2 = slice(arr);
  15. arr2[0] = 2;
  16. arr2[1] = 3;
  17. assert.deepEqual(arr, new Uint8Array([1, 2, 3]));
  18. assert.deepEqual(arr2, new Uint8Array([2, 3, 3]));
  19. assert.notEqual(arr.buffer, arr2.buffer);
  20. if (!Uint8Array.prototype.slice) {
  21. slice.shim();
  22. }
  23. var arr3 = arr.slice();
  24. arr3[0] = 2;
  25. arr3[1] = 3;
  26. assert.deepEqual(arr, new Uint8Array([1, 2, 3]));
  27. assert.deepEqual(arr3, new Uint8Array([2, 3, 3]));
  28. assert.notEqual(arr.buffer, arr3.buffer);
  29. ```
  30. ## Engines where this is needed
  31. - node v0.11.4 - v4: no prototype or own `slice` method
  32. - node < v0.11.3: own `slice` method that fails to clone the underlying buffer
  33. ## Tests
  34. Simply clone the repo, `npm install`, and run `npm test`
  35. [package-url]: https://npmjs.org/package/typedarray.prototype.slice
  36. [npm-version-svg]: https://versionbadg.es/es-shims/TypedArray.prototype.slice.svg
  37. [deps-svg]: https://david-dm.org/es-shims/TypedArray.prototype.slice.svg
  38. [deps-url]: https://david-dm.org/es-shims/TypedArray.prototype.slice
  39. [dev-deps-svg]: https://david-dm.org/es-shims/TypedArray.prototype.slice/dev-status.svg
  40. [dev-deps-url]: https://david-dm.org/es-shims/TypedArray.prototype.slice#info=devDependencies
  41. [npm-badge-png]: https://nodei.co/npm/typedarray.prototype.slice.png?downloads=true&stars=true
  42. [license-image]: https://img.shields.io/npm/l/typedarray.prototype.slice.svg
  43. [license-url]: LICENSE
  44. [downloads-image]: https://img.shields.io/npm/dm/typedarray.prototype.slice.svg
  45. [downloads-url]: https://npm-stat.com/charts.html?package=typedarray.prototype.slice
  46. [codecov-image]: https://codecov.io/gh/es-shims/TypedArray.prototype.slice/branch/main/graphs/badge.svg
  47. [codecov-url]: https://app.codecov.io/gh/es-shims/TypedArray.prototype.slice/
  48. [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/es-shims/TypedArray.prototype.slice
  49. [actions-url]: https://github.com/es-shims/TypedArray.prototype.slice/actions