nano-binary-search 1.0.0 → 1.0.2

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.
Files changed (3) hide show
  1. package/README.md +17 -5
  2. package/index.d.ts +19 -0
  3. package/package.json +14 -5
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.
@@ -68,15 +79,14 @@ Do you want to search a sub-array? Just pass in indices.
68
79
  * `lessFn` &mdash; function that takes three argument and returns a truthy value if the first argument
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
- * The function interface is modeled on callback function of array methods.
72
- * The signature: `lessFn(value, index, array) => boolean`.
82
+ * The function interface is modeled on the callback function of array methods.
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
 
76
86
  The function return an index, where we can safely insert the searched value with `splice()`:
77
87
 
78
- * if we used `<` operator, the index will point to the first value that is greater or equal than the searched value.
79
- * if we used `<=` operator, the index will point to the first value that is greater than the searched value.
88
+ * if we used `<` operator as the comparison function, the index will point to the first value that is greater or equal than the searched value.
89
+ * if we used `<=` operator as the comparison function, the index will point to the first value that is greater than the searched value.
80
90
 
81
91
  That's all Folks!
82
92
 
@@ -134,4 +144,6 @@ This project is licensed under the BSD-3-Clause license.
134
144
 
135
145
  ## Release history
136
146
 
147
+ - 1.0.2 *Improved docs*
148
+ - 1.0.1 *Added TS typings*
137
149
  - 1.0.0 *Initial release*
package/index.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ /**
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`.
4
+ */
5
+ type LessFn<T = any> = (value: T, index: number, sortedArray: readonly T[]) => boolean;
6
+
7
+ /**
8
+ * Binary search (see [docs](https://github.com/uhop/nano-binary-search#readme)):
9
+ * `binarySearch([1, 2, 3], x => x < 3)`
10
+ *
11
+ * @param sortedArray sorted array. It should be sorted in a compatible way with the comparison function.
12
+ * @param lessFn comparison function that takes a value, compares it with the pivot value, and returns a boolean. See {@link LessFn}.
13
+ * @param l left index. Defaults to 0.
14
+ * @param r right index. Defaults to `sortedArray.length`.
15
+ * @returns index where the pivot value can be inserted without breaking the sorted-ness of the array.
16
+ */
17
+ export declare function binarySearch<T>(sortedArray: readonly T[], lessFn: LessFn<T>, l?: number, r?: number): number;
18
+
19
+ export default binarySearch;
package/package.json CHANGED
@@ -1,11 +1,19 @@
1
1
  {
2
2
  "name": "nano-binary-search",
3
3
  "description": "Binary search for JavaScript done right.",
4
- "version": "1.0.0",
5
- "main": "index.js",
4
+ "version": "1.0.2",
6
5
  "type": "module",
6
+ "main": "index.js",
7
+ "types": "index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./index.d.ts",
11
+ "default": "./index.js"
12
+ }
13
+ },
7
14
  "scripts": {
8
- "test": "tape6 --flags FO"
15
+ "test": "tape6 --flags FO",
16
+ "ts-check": "tsc --noEmit"
9
17
  },
10
18
  "repository": {
11
19
  "type": "git",
@@ -26,7 +34,7 @@
26
34
  "author": "Eugene Lazutkin <eugene.lazutkin@gmail.com> (https://www.lazutkin.com/)",
27
35
  "license": "BSD-3-Clause",
28
36
  "files": [
29
- "index.js",
37
+ "index.*",
30
38
  "LICENSE",
31
39
  "README.md"
32
40
  ],
@@ -36,6 +44,7 @@
36
44
  ]
37
45
  },
38
46
  "devDependencies": {
39
- "tape-six": "^0.9.6"
47
+ "tape-six": "^0.9.6",
48
+ "typescript": "^5.5.4"
40
49
  }
41
50
  }