@tim-code/my-util 0.1.3 → 0.1.4
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/package.json +1 -1
- package/src/find.js +8 -2
- package/src/find.test.js +34 -0
package/package.json
CHANGED
package/src/find.js
CHANGED
|
@@ -159,14 +159,20 @@ export function findClosestGTE(array, desired, { key, map, threshold = Infinity
|
|
|
159
159
|
* @param {Array<T>} array
|
|
160
160
|
* @param {V} value The desired value to search for
|
|
161
161
|
* @param {Object} options
|
|
162
|
-
* @param {string=} options.key If specified, will consider the value for each element's key instead of the element itself.
|
|
162
|
+
* @param {string|number=} options.key If specified, will consider the value for each element's key instead of the element itself.
|
|
163
163
|
* @param {Function=} options.map If specified, will compute value by calling provided function on the element. Takes precedence over key.
|
|
164
|
+
* @param {string|number|Function=} options.transform Allows combining key and map as one parameter. Useful for piping in passed values.
|
|
164
165
|
* @param {string=} options.comparator "abs", "lt", "lte", "gt", "gte", "abs". Default is "abs" which implies T is number.
|
|
165
166
|
* @param {V=} options.threshold If specified, uses a different initial min/max/difference than positive or negative infinity.
|
|
166
167
|
* @returns {T|undefined}
|
|
167
168
|
*/
|
|
168
169
|
export function findClosest(array, value, options = {}) {
|
|
169
|
-
const { comparator = "abs" } = options
|
|
170
|
+
const { comparator = "abs", transform } = options
|
|
171
|
+
if (typeof transform === "function") {
|
|
172
|
+
options = { ...options, map: transform }
|
|
173
|
+
} else if (typeof transform === "string" || typeof transform === "number") {
|
|
174
|
+
options = { ...options, key: transform }
|
|
175
|
+
}
|
|
170
176
|
switch (comparator) {
|
|
171
177
|
case "lt":
|
|
172
178
|
return findClosestLT(array, value, options)
|
package/src/find.test.js
CHANGED
|
@@ -156,6 +156,40 @@ describe("findClosest", () => {
|
|
|
156
156
|
findClosest(arr, 4, { comparator: "abs", key: "length", threshold: -1 })
|
|
157
157
|
).toBeUndefined()
|
|
158
158
|
})
|
|
159
|
+
|
|
160
|
+
it("uses transform as a function (same as map)", () => {
|
|
161
|
+
const arr = [{ v: 1 }, { v: 5 }, { v: 10 }]
|
|
162
|
+
const mapFn = (el) => el.v
|
|
163
|
+
expect(findClosest(arr, 6, { transform: mapFn })).toEqual({ v: 5 })
|
|
164
|
+
// Should take precedence over key if both are present
|
|
165
|
+
expect(findClosest(arr, 6, { key: "notUsed", transform: mapFn })).toEqual({ v: 5 })
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
it("uses transform as a string (same as key)", () => {
|
|
169
|
+
const arr = [{ v: 1 }, { v: 5 }, { v: 10 }]
|
|
170
|
+
expect(findClosest(arr, 6, { transform: "v" })).toEqual({ v: 5 })
|
|
171
|
+
// Should take precedence over key if both are present
|
|
172
|
+
expect(findClosest(arr, 6, { key: "notUsed", transform: "v" })).toEqual({ v: 5 })
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
it("uses transform as a number (same as key)", () => {
|
|
176
|
+
const arr = [[1], [5], [10]]
|
|
177
|
+
expect(findClosest(arr, 6, { transform: 0 })).toEqual([5])
|
|
178
|
+
// Should take precedence over key if both are present
|
|
179
|
+
expect(findClosest(arr, 6, { key: "notUsed", transform: 0 })).toEqual([5])
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
it("transform does not override key if key is already present and transform is not provided", () => {
|
|
183
|
+
const arr = [{ v: 1 }, { v: 5 }, { v: 10 }]
|
|
184
|
+
expect(findClosest(arr, 6, { key: "v" })).toEqual({ v: 5 })
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
it("transform is ignored if not a function, string, or number", () => {
|
|
188
|
+
const arr = [{ v: 1 }, { v: 5 }, { v: 10 }]
|
|
189
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
190
|
+
expect(findClosest(arr, 6, { transform: null, key: "v" })).toEqual({ v: 5 })
|
|
191
|
+
expect(findClosest(arr, 6, { transform: {}, key: "v" })).toEqual({ v: 5 })
|
|
192
|
+
})
|
|
159
193
|
})
|
|
160
194
|
|
|
161
195
|
describe("findClosestAbs", () => {
|