Delete 100 files via Git Manager GUI
This commit is contained in:
-21
@@ -1,21 +0,0 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017-2026 Peculiar Ventures, LLC
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
-299
@@ -1,299 +0,0 @@
|
||||
# @peculiar/utils
|
||||
|
||||
[](https://www.npmjs.com/package/@peculiar/utils)
|
||||
[](https://github.com/PeculiarVentures/pvtsutils/actions/workflows/test.yml)
|
||||
[](https://coveralls.io/github/PeculiarVentures/pvtsutils?branch=master)
|
||||
[](https://github.com/PeculiarVentures/pvtsutils/blob/master/LICENSE)
|
||||
|
||||
Modern byte, text, converter registry, and PEM utilities for TypeScript projects.
|
||||
|
||||
The package is designed around a modular v2 API:
|
||||
|
||||
- multi-entry exports for tree-shake-friendly imports;
|
||||
- `encode` and `decode` terminology;
|
||||
- an extensible runtime converter registry;
|
||||
- generic PEM helpers without PKI-specific parsing;
|
||||
- a legacy compatibility layer for historical `pvtsutils` consumers.
|
||||
|
||||
## Install
|
||||
|
||||
```bash
|
||||
npm install @peculiar/utils
|
||||
```
|
||||
|
||||
## Entry Points
|
||||
|
||||
```ts
|
||||
import { bytes } from "@peculiar/utils";
|
||||
import { hex, base64, base64url } from "@peculiar/utils/encoding";
|
||||
import { pem } from "@peculiar/utils/pem";
|
||||
import { convert, createConverterRegistry, defaultConverters } from "@peculiar/utils/converters";
|
||||
import { Convert } from "@peculiar/utils/legacy";
|
||||
```
|
||||
|
||||
## Bytes Helpers
|
||||
|
||||
`@peculiar/utils/bytes` stays focused on stateless byte sequence utilities. It does not include stateful readers, writers, ASN.1 parsing, PDF parsing, or other structured binary readers. For structured binary parsing, use a dedicated binary reader package.
|
||||
|
||||
```ts
|
||||
import { bytes } from "@peculiar/utils";
|
||||
|
||||
const offset = bytes.indexOf(new Uint8Array([0x25, 0x25, 0x45, 0x4f, 0x46]), "%%EOF", {
|
||||
encoding: "ascii",
|
||||
});
|
||||
|
||||
const suffix = bytes.endsWith(new Uint8Array([0x25, 0x25, 0x45, 0x4f, 0x46]), "%%EOF", {
|
||||
encoding: "ascii",
|
||||
});
|
||||
```
|
||||
|
||||
### Find `startxref` In A PDF Tail
|
||||
|
||||
```ts
|
||||
import { lastIndexOf } from "@peculiar/utils/bytes";
|
||||
|
||||
const tailStart = Math.max(0, pdf.byteLength - 4096);
|
||||
|
||||
const offset = lastIndexOf(pdf, "startxref", {
|
||||
encoding: "ascii",
|
||||
start: pdf.byteLength,
|
||||
end: tailStart,
|
||||
});
|
||||
|
||||
if (offset === -1) {
|
||||
throw new Error("PDF startxref marker not found");
|
||||
}
|
||||
```
|
||||
|
||||
### Find `startxref` Via `tail`
|
||||
|
||||
```ts
|
||||
import { lastIndexOf, tail } from "@peculiar/utils/bytes";
|
||||
|
||||
const pdfTail = tail(pdf, 4096);
|
||||
|
||||
const localOffset = lastIndexOf(pdfTail, "startxref", {
|
||||
encoding: "ascii",
|
||||
});
|
||||
|
||||
const offset =
|
||||
localOffset === -1
|
||||
? -1
|
||||
: pdf.byteLength - pdfTail.byteLength + localOffset;
|
||||
```
|
||||
|
||||
### Check Prefixes And Suffixes
|
||||
|
||||
```ts
|
||||
import { bytes } from "@peculiar/utils";
|
||||
|
||||
bytes.startsWith(data, "-----BEGIN", { encoding: "ascii" });
|
||||
bytes.endsWith(data, "%%EOF", { encoding: "ascii" });
|
||||
```
|
||||
|
||||
### Compare Byte Sequences
|
||||
|
||||
```ts
|
||||
import { bytes } from "@peculiar/utils";
|
||||
|
||||
const result = bytes.compare(a, b);
|
||||
|
||||
if (result === 0) {
|
||||
console.log("equal");
|
||||
}
|
||||
```
|
||||
|
||||
## Convert API
|
||||
|
||||
The default `convert` facade is a convenience singleton backed by the built-in registry.
|
||||
|
||||
```ts
|
||||
import { convert } from "@peculiar/utils/converters";
|
||||
|
||||
const bytes = convert.decode("base64", "AQID");
|
||||
const text = convert.encode("hex", bytes, { case: "upper" });
|
||||
```
|
||||
|
||||
Deprecated `convert.to(...)` and `convert.from(...)` aliases are still available for temporary migration, but the primary v2 API is `encode` and `decode`.
|
||||
|
||||
## Transcode
|
||||
|
||||
Direct text-to-text transcoding goes through the registry without a manual intermediate step.
|
||||
|
||||
```ts
|
||||
import { convert } from "@peculiar/utils/converters";
|
||||
import { hex } from "@peculiar/utils/encoding";
|
||||
|
||||
const pemText = convert.transcode("AQID", {
|
||||
from: "base64",
|
||||
to: "pem",
|
||||
toOptions: {
|
||||
label: "CERTIFICATE",
|
||||
},
|
||||
});
|
||||
|
||||
const hexText = convert.transcode(pemText, {
|
||||
from: "pem",
|
||||
fromOptions: { label: "CERTIFICATE" },
|
||||
to: "hex",
|
||||
toOptions: hex.formats.colonUpper,
|
||||
});
|
||||
```
|
||||
|
||||
There is intentionally no chain API.
|
||||
|
||||
## Hex Formatting
|
||||
|
||||
The `hex` codec accepts common input styles and can format output explicitly.
|
||||
|
||||
```ts
|
||||
import { hex } from "@peculiar/utils/encoding";
|
||||
|
||||
hex.decode("0102030405060708090a0b0c");
|
||||
hex.decode("01020304 05060708 090a0b0c");
|
||||
hex.decode("01:02:03:04:05:06:07:08:09:0A:0B:0C");
|
||||
hex.decode("0x0102030405060708090a0b0c");
|
||||
|
||||
hex.encode(new Uint8Array([1, 2, 3, 4]), hex.formats.colonUpper);
|
||||
hex.encode(new Uint8Array([1, 2, 3, 4]), {
|
||||
prefix: "0x",
|
||||
group: {
|
||||
size: 2,
|
||||
separator: " ",
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
Available presets:
|
||||
|
||||
- `hex.formats.compact`
|
||||
- `hex.formats.upper`
|
||||
- `hex.formats.colon`
|
||||
- `hex.formats.colonUpper`
|
||||
- `hex.formats.groupsOf4`
|
||||
- `hex.formats.prefixed`
|
||||
|
||||
## Preserve Formatting
|
||||
|
||||
Use `parse` and `format` when you want to keep the original visual style of a hex string.
|
||||
|
||||
```ts
|
||||
import { hex } from "@peculiar/utils/encoding";
|
||||
|
||||
const parsed = hex.parse("01:02:03:04:05:06");
|
||||
|
||||
parsed.bytes;
|
||||
parsed.format;
|
||||
parsed.normalized;
|
||||
|
||||
const updated = hex.format(new Uint8Array([0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]), parsed.format);
|
||||
```
|
||||
|
||||
The same capabilities are available through the registry facade:
|
||||
|
||||
```ts
|
||||
import { convert } from "@peculiar/utils/converters";
|
||||
|
||||
const parsed = convert.parse("hex", "01:02:03:04");
|
||||
const formatted = convert.format("hex", new Uint8Array([0xaa, 0xbb, 0xcc, 0xdd]), parsed.format);
|
||||
```
|
||||
|
||||
## PEM Helpers
|
||||
|
||||
PEM support stays generic. The package does not parse ASN.1, validate PKI semantics, or handle encrypted PEM containers.
|
||||
|
||||
```ts
|
||||
import { pem } from "@peculiar/utils/pem";
|
||||
|
||||
const text = pem.encode("CERTIFICATE", new Uint8Array([1, 2, 3]));
|
||||
const blocks = pem.decode(text);
|
||||
const block = pem.find(text, "CERTIFICATE");
|
||||
const matches = pem.findAll(text, "CERTIFICATE");
|
||||
|
||||
const bundle = pem.encodeMany([
|
||||
{ label: "CERTIFICATE", data: new Uint8Array([1, 2, 3]) },
|
||||
{ label: "PRIVATE KEY", data: new Uint8Array([4, 5, 6]) },
|
||||
]);
|
||||
```
|
||||
|
||||
## Safe Decode And Detection
|
||||
|
||||
```ts
|
||||
import { convert } from "@peculiar/utils/converters";
|
||||
|
||||
const result = convert.tryDecode("hex", "01:02:03");
|
||||
|
||||
if (result.ok) {
|
||||
console.log(result.bytes);
|
||||
} else {
|
||||
console.error(result.error);
|
||||
}
|
||||
|
||||
const candidates = convert.detect("-----BEGIN DATA-----\nAQID\n-----END DATA-----\n", {
|
||||
formats: ["pem", "base64", "hex"],
|
||||
});
|
||||
```
|
||||
|
||||
## Custom Registries
|
||||
|
||||
Applications can create isolated registries instead of mutating global state.
|
||||
|
||||
```ts
|
||||
import { createConverterRegistry, defaultConverters } from "@peculiar/utils/converters";
|
||||
|
||||
const registry = createConverterRegistry(defaultConverters);
|
||||
|
||||
registry.register({
|
||||
name: "base58btc",
|
||||
aliases: ["b58"],
|
||||
encode(data) {
|
||||
return base58btcEncode(data);
|
||||
},
|
||||
decode(text) {
|
||||
return base58btcDecode(text);
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
Name and alias conflicts throw by default. Use `{ override: true }` only when replacement is intentional.
|
||||
|
||||
## Typed Converter Options
|
||||
|
||||
Built-in converters expose typed options through the registry facade.
|
||||
|
||||
```ts
|
||||
import { convert } from "@peculiar/utils/converters";
|
||||
|
||||
convert.encode("hex", new Uint8Array([1, 2, 3]), {
|
||||
case: "upper",
|
||||
});
|
||||
```
|
||||
|
||||
Wrong options are rejected by TypeScript:
|
||||
|
||||
```ts
|
||||
convert.encode("hex", new Uint8Array([1, 2, 3]), {
|
||||
label: "CERTIFICATE",
|
||||
});
|
||||
```
|
||||
|
||||
Custom converters can extend the options map via module augmentation:
|
||||
|
||||
```ts
|
||||
declare module "@peculiar/utils/converters" {
|
||||
interface ConverterOptionsMap {
|
||||
base58btc: {
|
||||
encode: Base58EncodeOptions;
|
||||
decode: Base58DecodeOptions;
|
||||
};
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Legacy Compatibility
|
||||
|
||||
The old `pvtsutils`-style surface is preserved under the legacy entry point.
|
||||
|
||||
```ts
|
||||
import { BufferSourceConverter, Convert, assign, combine, isEqual } from "@peculiar/utils/legacy";
|
||||
```
|
||||
-115
@@ -1,115 +0,0 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.isArrayBuffer = isArrayBuffer;
|
||||
exports.isSharedArrayBuffer = isSharedArrayBuffer;
|
||||
exports.isArrayBufferLike = isArrayBufferLike;
|
||||
exports.isArrayBufferView = isArrayBufferView;
|
||||
exports.isBufferSource = isBufferSource;
|
||||
exports.assertBufferSource = assertBufferSource;
|
||||
exports.toUint8Array = toUint8Array;
|
||||
exports.toUint8ArrayCopy = toUint8ArrayCopy;
|
||||
exports.toArrayBuffer = toArrayBuffer;
|
||||
exports.toArrayBufferLike = toArrayBufferLike;
|
||||
exports.toView = toView;
|
||||
exports.toViewCopy = toViewCopy;
|
||||
const ARRAY_BUFFER_TAG = "[object ArrayBuffer]";
|
||||
const SHARED_ARRAY_BUFFER_TAG = "[object SharedArrayBuffer]";
|
||||
function tagOf(value) {
|
||||
return Object.prototype.toString.call(value);
|
||||
}
|
||||
function isDataViewConstructor(type) {
|
||||
return type === DataView || type.prototype instanceof DataView;
|
||||
}
|
||||
function bytesPerElement(type) {
|
||||
if (isDataViewConstructor(type)) {
|
||||
return 1;
|
||||
}
|
||||
const value = type.BYTES_PER_ELEMENT;
|
||||
return value ?? 1;
|
||||
}
|
||||
function isArrayBufferViewLike(value) {
|
||||
if (ArrayBuffer.isView(value)) {
|
||||
return true;
|
||||
}
|
||||
if (!value || typeof value !== "object") {
|
||||
return false;
|
||||
}
|
||||
const view = value;
|
||||
return typeof view.byteOffset === "number"
|
||||
&& typeof view.byteLength === "number"
|
||||
&& isArrayBufferLike(view.buffer);
|
||||
}
|
||||
function copyBytes(data) {
|
||||
const view = toUint8Array(data);
|
||||
const copy = new Uint8Array(view.byteLength);
|
||||
copy.set(view);
|
||||
return copy;
|
||||
}
|
||||
function isArrayBuffer(value) {
|
||||
return tagOf(value) === ARRAY_BUFFER_TAG;
|
||||
}
|
||||
function isSharedArrayBuffer(value) {
|
||||
return typeof SharedArrayBuffer !== "undefined" && tagOf(value) === SHARED_ARRAY_BUFFER_TAG;
|
||||
}
|
||||
function isArrayBufferLike(value) {
|
||||
return isArrayBuffer(value) || isSharedArrayBuffer(value);
|
||||
}
|
||||
function isArrayBufferView(value) {
|
||||
return isArrayBufferViewLike(value);
|
||||
}
|
||||
function isBufferSource(value) {
|
||||
return isArrayBufferLike(value) || isArrayBufferView(value);
|
||||
}
|
||||
function assertBufferSource(value) {
|
||||
if (!isBufferSource(value)) {
|
||||
throw new TypeError("Expected ArrayBuffer, SharedArrayBuffer, or ArrayBufferView");
|
||||
}
|
||||
}
|
||||
function toUint8Array(data) {
|
||||
assertBufferSource(data);
|
||||
if (isArrayBufferLike(data)) {
|
||||
return new Uint8Array(data);
|
||||
}
|
||||
return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
||||
}
|
||||
function toUint8ArrayCopy(data) {
|
||||
return copyBytes(data);
|
||||
}
|
||||
function toArrayBuffer(data) {
|
||||
assertBufferSource(data);
|
||||
if (isArrayBuffer(data)) {
|
||||
return data;
|
||||
}
|
||||
const buffer = new ArrayBuffer(data.byteLength);
|
||||
new Uint8Array(buffer).set(toUint8Array(data));
|
||||
return buffer;
|
||||
}
|
||||
function toArrayBufferLike(data) {
|
||||
assertBufferSource(data);
|
||||
if (isArrayBufferLike(data)) {
|
||||
return data;
|
||||
}
|
||||
if (data.byteOffset === 0 && data.byteLength === data.buffer.byteLength) {
|
||||
return data.buffer;
|
||||
}
|
||||
return copyBytes(data).buffer;
|
||||
}
|
||||
function toView(data, type) {
|
||||
assertBufferSource(data);
|
||||
if (ArrayBuffer.isView(data) && data.constructor === type) {
|
||||
return data;
|
||||
}
|
||||
const view = toUint8Array(data);
|
||||
const elementSize = bytesPerElement(type);
|
||||
if (view.byteOffset % elementSize !== 0 || view.byteLength % elementSize !== 0) {
|
||||
throw new RangeError(`Cannot create ${type.name} over unaligned byte range`);
|
||||
}
|
||||
if (isDataViewConstructor(type)) {
|
||||
return new type(view.buffer, view.byteOffset, view.byteLength);
|
||||
}
|
||||
return new type(view.buffer, view.byteOffset, view.byteLength / elementSize);
|
||||
}
|
||||
function toViewCopy(data, type) {
|
||||
const copy = toUint8ArrayCopy(data);
|
||||
return toView(copy, type);
|
||||
}
|
||||
-41
@@ -1,41 +0,0 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.concatToUint8Array = concatToUint8Array;
|
||||
exports.concat = concat;
|
||||
const buffer_source_js_1 = require("./buffer-source.js");
|
||||
function concatToUint8Array(buffers) {
|
||||
const views = [];
|
||||
let length = 0;
|
||||
for (const buffer of buffers) {
|
||||
const view = (0, buffer_source_js_1.toUint8Array)(buffer);
|
||||
views.push(view);
|
||||
length += view.byteLength;
|
||||
}
|
||||
const result = new Uint8Array(length);
|
||||
let offset = 0;
|
||||
for (const view of views) {
|
||||
result.set(view, offset);
|
||||
offset += view.byteLength;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
function concat(first, second, ...rest) {
|
||||
let buffers;
|
||||
let type;
|
||||
if (typeof second === "function") {
|
||||
buffers = Array.from(first);
|
||||
type = second;
|
||||
}
|
||||
else if ((0, buffer_source_js_1.isBufferSource)(first)) {
|
||||
buffers = [first, second, ...rest].filter(buffer_source_js_1.isBufferSource);
|
||||
}
|
||||
else {
|
||||
buffers = Array.from(first);
|
||||
if (second) {
|
||||
buffers.push(second);
|
||||
}
|
||||
buffers.push(...rest);
|
||||
}
|
||||
const bytes = concatToUint8Array(buffers);
|
||||
return type ? (0, buffer_source_js_1.toView)(bytes, type) : bytes.buffer;
|
||||
}
|
||||
-17
@@ -1,17 +0,0 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.equal = equal;
|
||||
const buffer_source_js_1 = require("./buffer-source.js");
|
||||
function equal(a, b, options = {}) {
|
||||
const left = (0, buffer_source_js_1.toUint8Array)(a);
|
||||
const right = (0, buffer_source_js_1.toUint8Array)(b);
|
||||
if (!options.constantTime && left.byteLength !== right.byteLength) {
|
||||
return false;
|
||||
}
|
||||
const length = Math.max(left.byteLength, right.byteLength);
|
||||
let diff = left.byteLength ^ right.byteLength;
|
||||
for (let i = 0; i < length; i++) {
|
||||
diff |= (left[i] ?? 0) ^ (right[i] ?? 0);
|
||||
}
|
||||
return diff === 0;
|
||||
}
|
||||
-31
@@ -1,31 +0,0 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.tail = exports.startsWith = exports.slice = exports.lastIndexOf = exports.indexOf = exports.includes = exports.endsWith = exports.copy = exports.compare = exports.equal = exports.concatToUint8Array = exports.concat = exports.toViewCopy = exports.toView = exports.toUint8ArrayCopy = exports.toUint8Array = exports.toArrayBufferLike = exports.toArrayBuffer = exports.isSharedArrayBuffer = exports.isBufferSource = exports.isArrayBufferView = exports.isArrayBufferLike = exports.isArrayBuffer = exports.assertBufferSource = void 0;
|
||||
var buffer_source_js_1 = require("./buffer-source.js");
|
||||
Object.defineProperty(exports, "assertBufferSource", { enumerable: true, get: function () { return buffer_source_js_1.assertBufferSource; } });
|
||||
Object.defineProperty(exports, "isArrayBuffer", { enumerable: true, get: function () { return buffer_source_js_1.isArrayBuffer; } });
|
||||
Object.defineProperty(exports, "isArrayBufferLike", { enumerable: true, get: function () { return buffer_source_js_1.isArrayBufferLike; } });
|
||||
Object.defineProperty(exports, "isArrayBufferView", { enumerable: true, get: function () { return buffer_source_js_1.isArrayBufferView; } });
|
||||
Object.defineProperty(exports, "isBufferSource", { enumerable: true, get: function () { return buffer_source_js_1.isBufferSource; } });
|
||||
Object.defineProperty(exports, "isSharedArrayBuffer", { enumerable: true, get: function () { return buffer_source_js_1.isSharedArrayBuffer; } });
|
||||
Object.defineProperty(exports, "toArrayBuffer", { enumerable: true, get: function () { return buffer_source_js_1.toArrayBuffer; } });
|
||||
Object.defineProperty(exports, "toArrayBufferLike", { enumerable: true, get: function () { return buffer_source_js_1.toArrayBufferLike; } });
|
||||
Object.defineProperty(exports, "toUint8Array", { enumerable: true, get: function () { return buffer_source_js_1.toUint8Array; } });
|
||||
Object.defineProperty(exports, "toUint8ArrayCopy", { enumerable: true, get: function () { return buffer_source_js_1.toUint8ArrayCopy; } });
|
||||
Object.defineProperty(exports, "toView", { enumerable: true, get: function () { return buffer_source_js_1.toView; } });
|
||||
Object.defineProperty(exports, "toViewCopy", { enumerable: true, get: function () { return buffer_source_js_1.toViewCopy; } });
|
||||
var concat_js_1 = require("./concat.js");
|
||||
Object.defineProperty(exports, "concat", { enumerable: true, get: function () { return concat_js_1.concat; } });
|
||||
Object.defineProperty(exports, "concatToUint8Array", { enumerable: true, get: function () { return concat_js_1.concatToUint8Array; } });
|
||||
var equal_js_1 = require("./equal.js");
|
||||
Object.defineProperty(exports, "equal", { enumerable: true, get: function () { return equal_js_1.equal; } });
|
||||
var sequence_js_1 = require("./sequence.js");
|
||||
Object.defineProperty(exports, "compare", { enumerable: true, get: function () { return sequence_js_1.compare; } });
|
||||
Object.defineProperty(exports, "copy", { enumerable: true, get: function () { return sequence_js_1.copy; } });
|
||||
Object.defineProperty(exports, "endsWith", { enumerable: true, get: function () { return sequence_js_1.endsWith; } });
|
||||
Object.defineProperty(exports, "includes", { enumerable: true, get: function () { return sequence_js_1.includes; } });
|
||||
Object.defineProperty(exports, "indexOf", { enumerable: true, get: function () { return sequence_js_1.indexOf; } });
|
||||
Object.defineProperty(exports, "lastIndexOf", { enumerable: true, get: function () { return sequence_js_1.lastIndexOf; } });
|
||||
Object.defineProperty(exports, "slice", { enumerable: true, get: function () { return sequence_js_1.slice; } });
|
||||
Object.defineProperty(exports, "startsWith", { enumerable: true, get: function () { return sequence_js_1.startsWith; } });
|
||||
Object.defineProperty(exports, "tail", { enumerable: true, get: function () { return sequence_js_1.tail; } });
|
||||
-161
@@ -1,161 +0,0 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.indexOf = indexOf;
|
||||
exports.lastIndexOf = lastIndexOf;
|
||||
exports.includes = includes;
|
||||
exports.startsWith = startsWith;
|
||||
exports.endsWith = endsWith;
|
||||
exports.slice = slice;
|
||||
exports.tail = tail;
|
||||
exports.copy = copy;
|
||||
exports.compare = compare;
|
||||
const buffer_source_js_1 = require("./buffer-source.js");
|
||||
function clampIndex(value, fallback, length) {
|
||||
const normalized = Number.isFinite(value) ? Math.trunc(value) : fallback;
|
||||
if (normalized <= 0) {
|
||||
return 0;
|
||||
}
|
||||
if (normalized >= length) {
|
||||
return length;
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
function normalizeForwardRange(length, options) {
|
||||
const start = clampIndex(options?.start, 0, length);
|
||||
const end = clampIndex(options?.end, length, length);
|
||||
return end >= start ? [start, end] : [start, start];
|
||||
}
|
||||
function normalizeReverseRange(length, options) {
|
||||
const start = clampIndex(options?.start, length, length);
|
||||
const end = clampIndex(options?.end, 0, length);
|
||||
return start >= end ? [end, start] : [start, start];
|
||||
}
|
||||
function normalizeSliceIndex(value, fallback, length) {
|
||||
const normalized = Number.isFinite(value) ? Math.trunc(value) : fallback;
|
||||
if (normalized < 0) {
|
||||
return Math.max(length + normalized, 0);
|
||||
}
|
||||
if (normalized > length) {
|
||||
return length;
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
function encodeAscii(text) {
|
||||
const bytes = new Uint8Array(text.length);
|
||||
for (let i = 0; i < text.length; i++) {
|
||||
bytes[i] = text.charCodeAt(i) & 0xff;
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
function encodeUtf8(text) {
|
||||
return new TextEncoder().encode(text);
|
||||
}
|
||||
function toPatternBytes(pattern, options) {
|
||||
if (typeof pattern === "string") {
|
||||
return options?.encoding === "utf8" ? encodeUtf8(pattern) : encodeAscii(pattern);
|
||||
}
|
||||
return (0, buffer_source_js_1.toUint8Array)(pattern);
|
||||
}
|
||||
function bytesEqualAt(data, pattern, offset) {
|
||||
for (let index = 0; index < pattern.byteLength; index++) {
|
||||
if (data[offset + index] !== pattern[index]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
function indexOf(data, pattern, options) {
|
||||
const bytes = (0, buffer_source_js_1.toUint8Array)(data);
|
||||
const needle = toPatternBytes(pattern, options);
|
||||
const [start, end] = normalizeForwardRange(bytes.byteLength, options);
|
||||
if (needle.byteLength === 0) {
|
||||
return start;
|
||||
}
|
||||
const lastOffset = end - needle.byteLength;
|
||||
if (lastOffset < start) {
|
||||
return -1;
|
||||
}
|
||||
for (let offset = start; offset <= lastOffset; offset++) {
|
||||
if (bytesEqualAt(bytes, needle, offset)) {
|
||||
return offset;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
function lastIndexOf(data, pattern, options) {
|
||||
const bytes = (0, buffer_source_js_1.toUint8Array)(data);
|
||||
const needle = toPatternBytes(pattern, options);
|
||||
const [end, start] = normalizeReverseRange(bytes.byteLength, options);
|
||||
if (needle.byteLength === 0) {
|
||||
return start;
|
||||
}
|
||||
const firstOffset = start - needle.byteLength;
|
||||
if (firstOffset < end) {
|
||||
return -1;
|
||||
}
|
||||
for (let offset = firstOffset; offset >= end; offset--) {
|
||||
if (bytesEqualAt(bytes, needle, offset)) {
|
||||
return offset;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
function includes(data, pattern, options) {
|
||||
return indexOf(data, pattern, options) !== -1;
|
||||
}
|
||||
function startsWith(data, pattern, options) {
|
||||
const bytes = (0, buffer_source_js_1.toUint8Array)(data);
|
||||
const needle = toPatternBytes(pattern, options);
|
||||
if (needle.byteLength > bytes.byteLength) {
|
||||
return false;
|
||||
}
|
||||
return bytesEqualAt(bytes, needle, 0);
|
||||
}
|
||||
function endsWith(data, pattern, options) {
|
||||
const bytes = (0, buffer_source_js_1.toUint8Array)(data);
|
||||
const needle = toPatternBytes(pattern, options);
|
||||
if (needle.byteLength > bytes.byteLength) {
|
||||
return false;
|
||||
}
|
||||
return bytesEqualAt(bytes, needle, bytes.byteLength - needle.byteLength);
|
||||
}
|
||||
function slice(data, start, end) {
|
||||
const bytes = (0, buffer_source_js_1.toUint8Array)(data);
|
||||
const normalizedStart = normalizeSliceIndex(start, 0, bytes.byteLength);
|
||||
const normalizedEnd = normalizeSliceIndex(end, bytes.byteLength, bytes.byteLength);
|
||||
if (normalizedEnd <= normalizedStart) {
|
||||
return bytes.subarray(normalizedStart, normalizedStart);
|
||||
}
|
||||
return bytes.subarray(normalizedStart, normalizedEnd);
|
||||
}
|
||||
function tail(data, length) {
|
||||
const bytes = (0, buffer_source_js_1.toUint8Array)(data);
|
||||
const normalizedLength = Number.isFinite(length) ? Math.max(0, Math.trunc(length)) : 0;
|
||||
if (normalizedLength >= bytes.byteLength) {
|
||||
return bytes;
|
||||
}
|
||||
return bytes.subarray(bytes.byteLength - normalizedLength);
|
||||
}
|
||||
function copy(data) {
|
||||
return (0, buffer_source_js_1.toUint8ArrayCopy)(data);
|
||||
}
|
||||
function compare(a, b) {
|
||||
const left = (0, buffer_source_js_1.toUint8Array)(a);
|
||||
const right = (0, buffer_source_js_1.toUint8Array)(b);
|
||||
const limit = Math.min(left.byteLength, right.byteLength);
|
||||
for (let index = 0; index < limit; index++) {
|
||||
if (left[index] < right[index]) {
|
||||
return -1;
|
||||
}
|
||||
if (left[index] > right[index]) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
if (left.byteLength < right.byteLength) {
|
||||
return -1;
|
||||
}
|
||||
if (left.byteLength > right.byteLength) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user