|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
1007 bytes
|
| Line | |
|---|
| 1 | 'use strict';
|
|---|
| 2 | var globalThis = require('../internals/global-this');
|
|---|
| 3 | var uncurryThis = require('../internals/function-uncurry-this');
|
|---|
| 4 |
|
|---|
| 5 | var Uint8Array = globalThis.Uint8Array;
|
|---|
| 6 | var SyntaxError = globalThis.SyntaxError;
|
|---|
| 7 | var min = Math.min;
|
|---|
| 8 | var stringMatch = uncurryThis(''.match);
|
|---|
| 9 |
|
|---|
| 10 | module.exports = function (string, into) {
|
|---|
| 11 | var stringLength = string.length;
|
|---|
| 12 | if (stringLength % 2 !== 0) throw new SyntaxError('String should be an even number of characters');
|
|---|
| 13 | var maxLength = into ? min(into.length, stringLength / 2) : stringLength / 2;
|
|---|
| 14 | var bytes = into || new Uint8Array(maxLength);
|
|---|
| 15 | var segments = stringMatch(string, /.{2}/g);
|
|---|
| 16 | var written = 0;
|
|---|
| 17 | for (; written < maxLength; written++) {
|
|---|
| 18 | var result = +('0x' + segments[written] + '0');
|
|---|
| 19 | // eslint-disable-next-line no-self-compare -- NaN check
|
|---|
| 20 | if (result !== result) {
|
|---|
| 21 | throw new SyntaxError('String should only contain hex characters');
|
|---|
| 22 | }
|
|---|
| 23 | bytes[written] = result >> 4;
|
|---|
| 24 | }
|
|---|
| 25 | return { bytes: bytes, read: written << 1 };
|
|---|
| 26 | };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.