Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
taichunmin committed May 3, 2024
1 parent f79d1c7 commit c50e277
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/Uint8Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export interface Uint8Array {
join(separator?: string): string

/**
* Returns an list of keys in the array
* Creates and returns an iterator of buf keys (indexes).
*/
keys(): IterableIterator<number>

Expand Down
31 changes: 26 additions & 5 deletions lib/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1062,9 +1062,10 @@ export class Buffer extends Uint8Array {
/**
* If `value` is:
* - a `Buffer` or `Uint8Array`, `value` will be used in its entirety. To compare a partial `Buffer`, use `buf.subarray`.
* - a number, `value` will be interpreted as an unsigned 8-bit integer value between `0` and `255`.
* @param value - What to search for.
* @param byteOffset - Where to begin searching in `buf`. If negative, then offset is calculated from the end of `buf`. Default: `0`.
* - a number, `value` will be coerced to an unsigned 8-bit integer value between `0` and `255`.
* - not a string, number, or `Buffer`, this method will throw a `TypeError`.
* @param value - What to search for. If value is an empty string or empty Buffer and byteOffset is less than buf.length, byteOffset will be returned. If value is empty and byteOffset is at least buf.length, buf.length will be returned.
* @param byteOffset - Where to begin searching in `buf`. If negative, then offset is calculated from the end of `buf`. Default: `0`. If byteOffset is not a number, it will be coerced to a number. If the result of coercion is `NaN` or `0`, then the entire buffer will be searched. This behavior matches `String.prototype.indexOf()`.
* @param encoding - The encoding of `value`. Default: `'utf8'`.
* @returns The index of the first occurrence of `value` in `buf`, or `-1` if `buf` does not contain value.
* @example
Expand All @@ -1085,13 +1086,33 @@ export class Buffer extends Uint8Array {
* console.log(utf16Buffer.indexOf('\u03a3', -4, 'utf16le')) // Prints: 6
* })()
* ```
* @example
* ```js
* ;(async function () {
* const { Buffer } = await import('https://cdn.jsdelivr.net/npm/@taichunmin/buffer@0/dist/buffer.mjs/+esm')
*
* const b = Buffer.from('abcdef')
*
* // Passing a value that's a number, but not a valid byte.
* // Prints: 2, equivalent to searching for 99 or 'c'.
* console.log(b.indexOf(99.9))
* console.log(b.indexOf(256 + 99))
*
* // Passing a byteOffset that coerces to NaN or 0.
* // Prints: 1, searching the whole buffer.
* console.log(b.indexOf('b', undefined))
* console.log(b.indexOf('b', {}))
* console.log(b.indexOf('b', null))
* console.log(b.indexOf('b', []))
* })()
* ```
*/
indexOf (value: Buffer | Uint8Array | number, byteOffset?: number): number

/**
* `value` is interpreted according to the character encoding in `encoding`.
* @param value - What to search for.
* @param byteOffset - Where to begin searching in `buf`. If negative, then offset is calculated from the end of `buf`. Default: `0`.
* @param value - What to search for. If value is an empty string or empty Buffer and byteOffset is less than buf.length, byteOffset will be returned. If value is empty and byteOffset is at least buf.length, buf.length will be returned.
* @param byteOffset - Where to begin searching in `buf`. If negative, then offset is calculated from the end of `buf`. Default: `0`. If byteOffset is not a number, it will be coerced to a number. If the result of coercion is `NaN` or `0`, then the entire buffer will be searched. This behavior matches `String.prototype.indexOf()`.
* @param encoding - The encoding of `value`. Default: `'utf8'`.
* @returns The index of the first occurrence of `value` in `buf`, or `-1` if `buf` does not contain value.
*/
Expand Down

0 comments on commit c50e277

Please sign in to comment.