nano-binary-search 1.0.1 → 1.0.3

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
@@ -7,6 +7,8 @@ This is a nano binary search implementation. It is a tiny single file with no de
7
7
  The only reason I wrote it because I wrote it countless times before, I think it is perfect now
8
8
  because it is done right and fits JavaScript — it is ripe for code reuse.
9
9
 
10
+ For TypeScript users the typings are included.
11
+
10
12
  ## Why?
11
13
 
12
14
  Why do I think it is done right? Because it supports important invariants with
@@ -60,7 +62,16 @@ Do you want to search a sub-array? Just pass in indices.
60
62
 
61
63
  ## API
62
64
 
63
- `binarySearch(sortedArray, lessFn, l = 0, r = sortedArray.length)`:
65
+ The TypeScript-like API is as follows:
66
+
67
+ ```ts
68
+ const index: number = binarySearch<T>(
69
+ sortedArray: readonly T[],
70
+ lessFn: (value: T, index: number, array: readonly T[]) => boolean,
71
+ l: number = 0,
72
+ r: number = sortedArray.length
73
+ ): boolean;
74
+ ```
64
75
 
65
76
  * Inputs:
66
77
  * `sortedArray` &mdash; sorted array of some values. We don't care about values in this array.
@@ -69,7 +80,6 @@ Do you want to search a sub-array? Just pass in indices.
69
80
  (a value from array) is less than our value, whatever it is. The second value is its index,
70
81
  and the third is the `sortedArray`.
71
82
  * The function interface is modeled on the callback function of array methods.
72
- * The signature: `lessFn(value: T, index: number, array: T[]) => boolean`.
73
83
  * `l` &mdash; left index. This index is inclusive. Defaults to 0.
74
84
  * `r` &mdash; right index. This index is exclusive. Defaults to `sortedArray.length`.
75
85
 
@@ -134,5 +144,7 @@ This project is licensed under the BSD-3-Clause license.
134
144
 
135
145
  ## Release history
136
146
 
137
- - 1.0.1 *Added TS typings*
138
- - 1.0.0 *Initial release*
147
+ * 1.0.3 *Added a reference to the TS types*
148
+ * 1.0.2 *Improved docs*
149
+ * 1.0.1 *Added TS typings*
150
+ * 1.0.0 *Initial release*
package/index.d.ts CHANGED
@@ -1,10 +1,12 @@
1
1
  /**
2
2
  * Comparison function type. It compares a value with some internal pivot value and returns a boolean.
3
+ * Additional arguments are `index` (the pivot index) and `sortedArray`.
3
4
  */
4
- type LessFn<T = any> = (x: T, index: number, array: readonly T[]) => boolean;
5
+ type LessFn<T = any> = (value: T, index: number, sortedArray: readonly T[]) => boolean;
5
6
 
6
7
  /**
7
- * Binary search.
8
+ * Binary search (see [docs](https://github.com/uhop/nano-binary-search#readme)):
9
+ * `binarySearch([1, 2, 3], x => x < 3)`
8
10
  *
9
11
  * @param sortedArray sorted array. It should be sorted in a compatible way with the comparison function.
10
12
  * @param lessFn comparison function that takes a value, compares it with the pivot value, and returns a boolean. See {@link LessFn}.
@@ -15,4 +17,3 @@ type LessFn<T = any> = (x: T, index: number, array: readonly T[]) => boolean;
15
17
  export declare function binarySearch<T>(sortedArray: readonly T[], lessFn: LessFn<T>, l?: number, r?: number): number;
16
18
 
17
19
  export default binarySearch;
18
- //# sourceMappingURL=index.d.ts.map
package/index.js CHANGED
@@ -1,3 +1,5 @@
1
+ // @ts-self-types="./index.d.ts"
2
+
1
3
  'use strict';
2
4
 
3
5
  export const binarySearch = (sortedArray, lessFn, l = 0, r = sortedArray.length) => {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nano-binary-search",
3
3
  "description": "Binary search for JavaScript done right.",
4
- "version": "1.0.1",
4
+ "version": "1.0.3",
5
5
  "type": "module",
6
6
  "main": "index.js",
7
7
  "types": "index.d.ts",
@@ -45,6 +45,6 @@
45
45
  },
46
46
  "devDependencies": {
47
47
  "tape-six": "^0.9.6",
48
- "typescript": "^5.5.4"
48
+ "typescript": "^5.6.2"
49
49
  }
50
50
  }