nano-binary-search 1.0.0 → 1.0.1

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 +5 -4
  2. package/index.d.ts +18 -0
  3. package/package.json +14 -5
package/README.md CHANGED
@@ -68,15 +68,15 @@ Do you want to search a sub-array? Just pass in indices.
68
68
  * `lessFn` — function that takes three argument and returns a truthy value if the first argument
69
69
  (a value from array) is less than our value, whatever it is. The second value is its index,
70
70
  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`.
71
+ * The function interface is modeled on the callback function of array methods.
72
+ * The signature: `lessFn(value: T, index: number, array: T[]) => boolean`.
73
73
  * `l` — left index. This index is inclusive. Defaults to 0.
74
74
  * `r` — right index. This index is exclusive. Defaults to `sortedArray.length`.
75
75
 
76
76
  The function return an index, where we can safely insert the searched value with `splice()`:
77
77
 
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.
78
+ * 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.
79
+ * if we used `<=` operator as the comparison function, the index will point to the first value that is greater than the searched value.
80
80
 
81
81
  That's all Folks!
82
82
 
@@ -134,4 +134,5 @@ This project is licensed under the BSD-3-Clause license.
134
134
 
135
135
  ## Release history
136
136
 
137
+ - 1.0.1 *Added TS typings*
137
138
  - 1.0.0 *Initial release*
package/index.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Comparison function type. It compares a value with some internal pivot value and returns a boolean.
3
+ */
4
+ type LessFn<T = any> = (x: T, index: number, array: readonly T[]) => boolean;
5
+
6
+ /**
7
+ * Binary search.
8
+ *
9
+ * @param sortedArray sorted array. It should be sorted in a compatible way with the comparison function.
10
+ * @param lessFn comparison function that takes a value, compares it with the pivot value, and returns a boolean. See {@link LessFn}.
11
+ * @param l left index. Defaults to 0.
12
+ * @param r right index. Defaults to `sortedArray.length`.
13
+ * @returns index where the pivot value can be inserted without breaking the sorted-ness of the array.
14
+ */
15
+ export declare function binarySearch<T>(sortedArray: readonly T[], lessFn: LessFn<T>, l?: number, r?: number): number;
16
+
17
+ export default binarySearch;
18
+ //# sourceMappingURL=index.d.ts.map
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.1",
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
  }