Skip to content

Commit

Permalink
v0.13.0: jsDelivr Purge + method aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
taichunmin committed May 31, 2024
1 parent 9512715 commit 0e86163
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 25 deletions.
14 changes: 13 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ jobs:
uses: actions/deploy-pages@v4
- name: 發布至 npm
uses: JS-DevTools/npm-publish@v3
id: npm-publish
with:
access: public
token: ${{ secrets.NPM_TOKEN }}
token: ${{ secrets.NPM_TOKEN }}
- name: jsDelivr Purge
if: ${{ steps.npm-publish.outputs.type }}
uses: egad13/purge-jsdelivr-cache@v1
with:
url: |
https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/dist/buffer.global.js
https://cdn.jsdelivr.net/npm/@taichunmin/buffer/dist/buffer.global.js
https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/dist/buffer.global.min.js
https://cdn.jsdelivr.net/npm/@taichunmin/buffer/dist/buffer.global.min.js
https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/dist/buffer.mjs/+esm
https://cdn.jsdelivr.net/npm/@taichunmin/buffer/dist/buffer.mjs/+esm
22 changes: 2 additions & 20 deletions lib/Uint8Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,6 @@ export interface Uint8Array {
*/
map(callbackfn: (value: number, index: number, array: Uint8Array) => number, thisArg?: any): Uint8Array

/**
* Calls the specified callback function for all the elements in an array. The return value of
* the callback function is the accumulated result, and is provided as an argument in the next
* call to the callback function.
* @param callbackfn - A function that accepts up to four arguments. The reduce method calls the
* callbackfn function one time for each element in the array.
*/
reduce(callbackfn: reduceCallbackFn<number>): number

/**
* Calls the specified callback function for all the elements in an array. The return value of
* the callback function is the accumulated result, and is provided as an argument in the next
Expand All @@ -185,16 +176,7 @@ export interface Uint8Array {
* the accumulation. The first call to the callbackfn function provides this value as an argument
* instead of an array value.
*/
reduce<U = number>(callbackfn: reduceCallbackFn<U>, initialValue: U): U

/**
* Calls the specified callback function for all the elements in an array, in descending order.
* The return value of the callback function is the accumulated result, and is provided as an
* argument in the next call to the callback function.
* @param callbackfn - A function that accepts up to four arguments. The reduceRight method calls
* the callbackfn function one time for each element in the array.
*/
reduceRight(callbackfn: reduceCallbackFn<number>): number
reduce<T = number>(callbackfn: reduceCallbackFn<T>, initialValue?: T): T

/**
* Calls the specified callback function for all the elements in an array, in descending order.
Expand All @@ -206,7 +188,7 @@ export interface Uint8Array {
* the accumulation. The first call to the callbackfn function provides this value as an
* argument instead of an array value.
*/
reduceRight<U = number>(callbackfn: reduceCallbackFn<U>, initialValue: U): U
reduceRight<T = number>(callbackfn: reduceCallbackFn<T>, initialValue?: T): T

/**
* Reverses the elements in an Array.
Expand Down
26 changes: 26 additions & 0 deletions lib/buffer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1726,3 +1726,29 @@ describe('util.inspect()', () => {
expect(actual).toBe('<Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... 50 more bytes>')
})
})

describe('function alias test', () => {
test.each([
{ a: 'readUInt8', b: 'readUint8' },
{ a: 'readUInt16BE', b: 'readUint16BE' },
{ a: 'readUInt16LE', b: 'readUint16LE' },
{ a: 'readUInt32BE', b: 'readUint32BE' },
{ a: 'readUInt32LE', b: 'readUint32LE' },
{ a: 'readUIntBE', b: 'readUintBE' },
{ a: 'readUIntLE', b: 'readUintLE' },
{ a: 'readBigUInt64BE', b: 'readBigUint64BE' },
{ a: 'readBigUInt64LE', b: 'readBigUint64LE' },
{ a: 'writeUInt8', b: 'writeUint8' },
{ a: 'writeUInt16BE', b: 'writeUint16BE' },
{ a: 'writeUInt16LE', b: 'writeUint16LE' },
{ a: 'writeUInt32BE', b: 'writeUint32BE' },
{ a: 'writeUInt32LE', b: 'writeUint32LE' },
{ a: 'writeUIntBE', b: 'writeUintBE' },
{ a: 'writeUIntLE', b: 'writeUintLE' },
{ a: 'writeBigUInt64BE', b: 'writeBigUint64BE' },
{ a: 'writeBigUInt64LE', b: 'writeBigUint64LE' },
])('Buffer#$a should be alias of Buffer#$b', ({ a, b }) => {
const buf = new Buffer()
expect(buf[a as any]).toBe(buf[b as any])
})
})
112 changes: 109 additions & 3 deletions lib/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,12 @@ export class Buffer extends Uint8Array {
return this.#dv.getBigUint64(offset)
}

/**
* Alias of {@link Buffer.readBigUInt64BE}.
* @group Method Aliases
*/
get readBigUint64BE (): Buffer['readBigUInt64BE'] { return this.readBigUInt64BE }

/**
* Reads an unsigned, little-endian 64-bit integer from `buf` at the specified `offset`.
* @param offset - Number of bytes to skip before starting to read. Must satisfy: `0 <= offset <= buf.length - 8`. Default: `0`.
Expand All @@ -1513,6 +1519,12 @@ export class Buffer extends Uint8Array {
return this.#dv.getBigUint64(offset, true)
}

/**
* Alias of {@link Buffer.readBigUInt64LE}.
* @group Method Aliases
*/
get readBigUint64LE (): Buffer['readBigUInt64LE'] { return this.readBigUInt64LE }

/**
* Reads a 64-bit, big-endian double from `buf` at the specified `offset`.
* @param offset - Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 8`. Default: `0`.
Expand Down Expand Up @@ -1723,6 +1735,12 @@ export class Buffer extends Uint8Array {
return this.#dv.getUint8(offset)
}

/**
* Alias of {@link Buffer.readUInt8}.
* @group Method Aliases
*/
get readUint8 (): Buffer['readUInt8'] { return this.readUInt8 }

/**
* Reads an unsigned, big-endian 16-bit integer from `buf` at the specified `offset`.
* @param offset - Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 2`. Default: `0`.
Expand All @@ -1741,6 +1759,12 @@ export class Buffer extends Uint8Array {
return this.#dv.getUint16(offset)
}

/**
* Alias of {@link Buffer.readUInt16BE}.
* @group Method Aliases
*/
get readUint16BE (): Buffer['readUInt16BE'] { return this.readUInt16BE }

/**
* Reads an unsigned, little-endian 16-bit integer from `buf` at the specified `offset`.
* @param offset - Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 2`. Default: `0`.
Expand All @@ -1759,6 +1783,12 @@ export class Buffer extends Uint8Array {
return this.#dv.getUint16(offset, true)
}

/**
* Alias of {@link Buffer.readUInt16LE}.
* @group Method Aliases
*/
get readUint16LE (): Buffer['readUInt16LE'] { return this.readUInt16LE }

/**
* Reads an unsigned, big-endian 32-bit integer from `buf` at the specified `offset`.
* @param offset - Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 4`. Default: `0`.
Expand All @@ -1776,6 +1806,12 @@ export class Buffer extends Uint8Array {
return this.#dv.getUint32(offset)
}

/**
* Alias of {@link Buffer.readUInt32BE}.
* @group Method Aliases
*/
get readUint32BE (): Buffer['readUInt32BE'] { return this.readUInt32BE }

/**
* Reads an unsigned, little-endian 32-bit integer from `buf` at the specified `offset`.
* @param offset - Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 4`. Default: `0`.
Expand All @@ -1793,6 +1829,12 @@ export class Buffer extends Uint8Array {
return this.#dv.getUint32(offset, true)
}

/**
* Alias of {@link Buffer.readUInt32LE}.
* @group Method Aliases
*/
get readUint32LE (): Buffer['readUInt32LE'] { return this.readUInt32LE }

/**
* Reads `byteLength` number of bytes from `buf` at the specified `offset` and interprets the result as an unsigned big-endian integer supporting up to 48 bits of accuracy.
* @param offset - Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - byteLength`.
Expand All @@ -1815,6 +1857,12 @@ export class Buffer extends Uint8Array {
return tmp
}

/**
* Alias of {@link Buffer.readUIntBE}.
* @group Method Aliases
*/
get readUintBE (): Buffer['readUIntBE'] { return this.readUIntBE }

/**
* Reads `byteLength` number of bytes from `buf` at the specified `offset` and interprets the result as an unsigned big-endian integer supporting up to 48 bits of accuracy.
* @param offset - Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - byteLength`.
Expand All @@ -1837,6 +1885,12 @@ export class Buffer extends Uint8Array {
return tmp
}

/**
* Alias of {@link Buffer.readUIntLE}.
* @group Method Aliases
*/
get readUintLE (): Buffer['readUIntLE'] { return this.readUIntLE }

/**
* Reads a 16-bit, big-endian float from `buf` at the specified `offset`.
* @param offset - Number of bytes to skip before starting to read. Must satisfy `0 <= offset <= buf.length - 2`. Default: `0`.
Expand Down Expand Up @@ -2207,9 +2261,7 @@ export class Buffer extends Uint8Array {
static toBase64urlString (buf: Buffer): string {
const arr = []
for (let i = 0; i < buf.length; i += 3) {
const u24 = (buf[i] << 16) +
((i + 1 < buf.length ? buf[i + 1] : 0) << 8) +
(i + 2 < buf.length ? buf[i + 2] : 0)
const u24 = (buf[i] << 16) + ((buf[i + 1] ?? 0) << 8) + (buf[i + 2] ?? 0)
arr.push(...[
CHARCODE_BASE64URL.get(u24 >>> 18 & 0x3F),
CHARCODE_BASE64URL.get(u24 >>> 12 & 0x3F),
Expand Down Expand Up @@ -2344,6 +2396,12 @@ export class Buffer extends Uint8Array {
return this
}

/**
* Alias of {@link Buffer.writeBigUInt64BE}.
* @group Method Aliases
*/
get writeBigUint64BE (): Buffer['writeBigUInt64BE'] { return this.writeBigUInt64BE }

/**
* Writes `value` to `buf` at the specified `offset` as little-endian.
* @param val - Number to be written to `buf`.
Expand All @@ -2364,6 +2422,12 @@ export class Buffer extends Uint8Array {
return this
}

/**
* Alias of {@link Buffer.writeBigUInt64LE}.
* @group Method Aliases
*/
get writeBigUint64LE (): Buffer['writeBigUInt64LE'] { return this.writeBigUInt64LE }

/**
* Writes `value` to `buf` at the specified `offset` as big-endian. The `value` must be a JavaScript number. Behavior is undefined when `value` is anything other than a JavaScript number.
* @param val - Number to be written to `buf`.
Expand Down Expand Up @@ -2656,6 +2720,12 @@ export class Buffer extends Uint8Array {
return this
}

/**
* Alias of {@link Buffer.writeUInt8}.
* @group Method Aliases
*/
get writeUint8 (): Buffer['writeUInt8'] { return this.writeUInt8 }

/**
* Writes `value` to `buf` at the specified `offset` as big-endian. The `value` must be a valid unsigned 16-bit integer. Behavior is undefined when `value` is anything other than an unsigned 16-bit integer.
* @param val - Number to be written to `buf`.
Expand All @@ -2677,6 +2747,12 @@ export class Buffer extends Uint8Array {
return this
}

/**
* Alias of {@link Buffer.writeUInt16BE}.
* @group Method Aliases
*/
get writeUint16BE (): Buffer['writeUInt16BE'] { return this.writeUInt16BE }

/**
* Writes `value` to `buf` at the specified `offset` as little-endian. The `value` must be a valid unsigned 16-bit integer. Behavior is undefined when `value` is anything other than an unsigned 16-bit integer.
* @param val - Number to be written to `buf`.
Expand All @@ -2698,6 +2774,12 @@ export class Buffer extends Uint8Array {
return this
}

/**
* Alias of {@link Buffer.writeUInt16LE}.
* @group Method Aliases
*/
get writeUint16LE (): Buffer['writeUInt16LE'] { return this.writeUInt16LE }

/**
* Writes `value` to `buf` at the specified `offset` as big-endian. The `value` must be a valid unsigned 32-bit integer. Behavior is undefined when `value` is anything other than an unsigned 32-bit integer.
* @param val - Number to be written to `buf`.
Expand All @@ -2718,6 +2800,12 @@ export class Buffer extends Uint8Array {
return this
}

/**
* Alias of {@link Buffer.writeUInt32BE}.
* @group Method Aliases
*/
get writeUint32BE (): Buffer['writeUInt32BE'] { return this.writeUInt32BE }

/**
* Writes `value` to `buf` at the specified `offset` as little-endian. The `value` must be a valid unsigned 32-bit integer. Behavior is undefined when `value` is anything other than an unsigned 32-bit integer.
* @param val - Number to be written to `buf`.
Expand All @@ -2738,6 +2826,12 @@ export class Buffer extends Uint8Array {
return this
}

/**
* Alias of {@link Buffer.writeUInt32LE}.
* @group Method Aliases
*/
get writeUint32LE (): Buffer['writeUInt32LE'] { return this.writeUInt32LE }

/**
* Writes `byteLength` bytes of `value` to `buf` at the specified `offset` as big-endian. Supports up to 48 bits of accuracy. Behavior is undefined when `value` is anything other than an unsigned integer.
* @param val - Number to be written to `buf`.
Expand All @@ -2764,6 +2858,12 @@ export class Buffer extends Uint8Array {
return this
}

/**
* Alias of {@link Buffer.writeUIntBE}.
* @group Method Aliases
*/
get writeUintBE (): Buffer['writeUIntBE'] { return this.writeUIntBE }

/**
* Writes `byteLength` bytes of `value` to `buf` at the specified `offset` as little-endian. Supports up to 48 bits of accuracy. Behavior is undefined when `value` is anything other than an unsigned integer.
* @param val - Number to be written to `buf`.
Expand All @@ -2790,6 +2890,12 @@ export class Buffer extends Uint8Array {
return this
}

/**
* Alias of {@link Buffer.writeUIntLE}.
* @group Method Aliases
*/
get writeUintLE (): Buffer['writeUIntLE'] { return this.writeUIntLE }

/**
* Treats `buf` as a [most-significant bit](https://en.wikipedia.org/wiki/Most_significant_bit) array and write a bit at the specified bit offset.
* @param val - Bit value to write.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"main": "index.js",
"name": "@taichunmin/buffer",
"unpkg": "dist/buffer.global.js",
"version": "0.12.0",
"version": "0.13.0",
"author": {
"email": "taichunmin@gmail.com",
"name": "Chunmin Tai",
Expand Down

0 comments on commit 0e86163

Please sign in to comment.