nano-binary-search 1.0.8 → 1.0.9
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 +21 -20
- package/package.json +6 -4
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# nano-binary-search [![NPM version][npm-img]][npm-url]
|
|
2
2
|
|
|
3
|
-
[npm-img]:
|
|
4
|
-
[npm-url]:
|
|
3
|
+
[npm-img]: https://img.shields.io/npm/v/nano-binary-search.svg
|
|
4
|
+
[npm-url]: https://npmjs.org/package/nano-binary-search
|
|
5
5
|
|
|
6
6
|
This is a nano binary search implementation. It is a tiny single file with no dependencies.
|
|
7
7
|
The only reason I wrote it because I wrote it countless times before, I think it is perfect now
|
|
@@ -73,20 +73,20 @@ const index: number = binarySearch<T>(
|
|
|
73
73
|
): boolean;
|
|
74
74
|
```
|
|
75
75
|
|
|
76
|
-
|
|
77
|
-
|
|
76
|
+
- Inputs:
|
|
77
|
+
- `sortedArray` — sorted array of some values. We don't care about values in this array.
|
|
78
78
|
It is up to `lessFn` to compare them. The array should be sorted in a compatible way with `lessFn`.
|
|
79
|
-
|
|
79
|
+
- `lessFn` — function that takes three argument and returns a truthy value if the first argument
|
|
80
80
|
(a value from array) is less than our value, whatever it is. The second value is its index,
|
|
81
81
|
and the third is the `sortedArray`.
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
82
|
+
- The function interface is modeled on the callback function of array methods.
|
|
83
|
+
- `l` — left index. This index is inclusive. Defaults to 0.
|
|
84
|
+
- `r` — right index. This index is exclusive. Defaults to `sortedArray.length`.
|
|
85
85
|
|
|
86
86
|
The function return an index, where we can safely insert the searched value with `splice()`:
|
|
87
87
|
|
|
88
|
-
|
|
89
|
-
|
|
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.
|
|
90
90
|
|
|
91
91
|
That's all Folks!
|
|
92
92
|
|
|
@@ -128,7 +128,7 @@ For example (two argument version for simplicity):
|
|
|
128
128
|
const stringLessFn = (a, b) => a < b;
|
|
129
129
|
|
|
130
130
|
// comparator #1 (two comparisons)
|
|
131
|
-
const stringCompareFn1 = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
131
|
+
const stringCompareFn1 = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
|
|
132
132
|
|
|
133
133
|
// comparator #2: smarter (a method call)
|
|
134
134
|
const stringCompareFn2 = (a, b) => a.localeCompare(b);
|
|
@@ -144,12 +144,13 @@ This project is licensed under the BSD-3-Clause license.
|
|
|
144
144
|
|
|
145
145
|
## Release history
|
|
146
146
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
147
|
+
- 1.0.9 _Updated dev deps_
|
|
148
|
+
- 1.0.8 _Updated dev deps_
|
|
149
|
+
- 1.0.7 _Updated dev deps_
|
|
150
|
+
- 1.0.6 _Updated dev deps_
|
|
151
|
+
- 1.0.5 _Updated dev deps_
|
|
152
|
+
- 1.0.4 _Updated dev deps_
|
|
153
|
+
- 1.0.3 _Added a reference to the TS types_
|
|
154
|
+
- 1.0.2 _Improved docs_
|
|
155
|
+
- 1.0.1 _Added TS typings_
|
|
156
|
+
- 1.0.0 _Initial release_
|
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.
|
|
4
|
+
"version": "1.0.9",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"types": "index.d.ts",
|
|
@@ -19,7 +19,9 @@
|
|
|
19
19
|
"test:proc": "tape6-proc --flags FO",
|
|
20
20
|
"test:proc:bun": "bun run `npx tape6-proc --self` --flags FO",
|
|
21
21
|
"test:proc:deno": "deno run -A `npx tape6-proc --self` --flags FO -r --allow-env",
|
|
22
|
-
"ts-check": "tsc --noEmit"
|
|
22
|
+
"ts-check": "tsc --noEmit",
|
|
23
|
+
"lint": "prettier --check .",
|
|
24
|
+
"lint:fix": "prettier --write ."
|
|
23
25
|
},
|
|
24
26
|
"repository": {
|
|
25
27
|
"type": "git",
|
|
@@ -56,8 +58,8 @@
|
|
|
56
58
|
}
|
|
57
59
|
},
|
|
58
60
|
"devDependencies": {
|
|
59
|
-
"tape-six": "^1.4.
|
|
60
|
-
"tape-six-proc": "^1.1.
|
|
61
|
+
"tape-six": "^1.4.5",
|
|
62
|
+
"tape-six-proc": "^1.1.6",
|
|
61
63
|
"typescript": "^5.9.3"
|
|
62
64
|
}
|
|
63
65
|
}
|