@thi.ng/arrays 2.13.16 → 2.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -60,7 +60,7 @@ For Node.js REPL:
60
60
  const arr = await import("@thi.ng/arrays");
61
61
  ```
62
62
 
63
- Package sizes (brotli'd, pre-treeshake): ESM: 3.17 KB
63
+ Package sizes (brotli'd, pre-treeshake): ESM: 3.23 KB
64
64
 
65
65
  ## Dependencies
66
66
 
@@ -23,4 +23,15 @@ import type { TypedArray } from "@thi.ng/api";
23
23
  */
24
24
  export declare function findSequence<T>(buf: Array<T>, needle: ArrayLike<T>, start?: number): number;
25
25
  export declare function findSequence(buf: TypedArray, needle: ArrayLike<number>, start?: number): number;
26
+ /**
27
+ * Similar to {@link findSequence}, but searches backwards, starting form given
28
+ * `start` index (default: end of `buf` array). Returns index of `needle` in
29
+ * `buf` or -1 if unsuccessful.
30
+ *
31
+ * @param buf
32
+ * @param needle
33
+ * @param start
34
+ */
35
+ export declare function findLastSequence<T>(buf: Array<T>, needle: ArrayLike<T>, start?: number): number;
36
+ export declare function findLastSequence(buf: TypedArray, needle: ArrayLike<number>, start?: number): number;
26
37
  //# sourceMappingURL=find-sequence.d.ts.map
package/find-sequence.js CHANGED
@@ -2,7 +2,7 @@ function findSequence(buf, needle, start = 0) {
2
2
  const m = buf.length;
3
3
  const n = needle.length;
4
4
  const max = m - n;
5
- if (max < 0) return -1;
5
+ if (!n || max < 0) return -1;
6
6
  while (start < m) {
7
7
  const idx = buf.indexOf(needle[0], start);
8
8
  if (idx < 0 || idx > max) return -1;
@@ -17,6 +17,21 @@ function findSequence(buf, needle, start = 0) {
17
17
  }
18
18
  return -1;
19
19
  }
20
+ function findLastSequence(buf, needle, start = buf.length - 1) {
21
+ const m = buf.length;
22
+ const n = needle.length;
23
+ if (!n || n > m) return -1;
24
+ start = Math.min(start, m - n);
25
+ let j;
26
+ for (let i = start; i >= 0; i--) {
27
+ for (j = n; j-- > 0; ) {
28
+ if (buf[i + j] !== needle[j]) break;
29
+ }
30
+ if (j < 0) return i;
31
+ }
32
+ return -1;
33
+ }
20
34
  export {
35
+ findLastSequence,
21
36
  findSequence
22
37
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/arrays",
3
- "version": "2.13.16",
3
+ "version": "2.14.0",
4
4
  "description": "Array / Arraylike utilities",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -187,5 +187,5 @@
187
187
  "tag": "array",
188
188
  "year": 2018
189
189
  },
190
- "gitHead": "d977f819bcafdcb2b24c45f8d01a167fe29fc85a\n"
190
+ "gitHead": "74b6f319d8f52c9266d97f616be5298a6a5b96e4\n"
191
191
  }