Comparing version 0.0.8 to 0.0.9
@@ -0,2 +1,3 @@ | ||
declare function findOnlyIndex<T>(array: ReadonlyArray<T>, predicate: (item: T) => boolean, message?: string): number; | ||
declare function findOnly<T>(array: ReadonlyArray<T>, predicate: (item: T) => boolean, message?: string): T; | ||
export { findOnly }; | ||
export { findOnlyIndex, findOnly }; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.findOnly = void 0; | ||
function findOnly(array, predicate, message) { | ||
const found = array.filter(predicate); | ||
if (found.length !== 1) { | ||
throw new Error(message !== null && message !== void 0 ? message : `Expected exactly one item, found ${found.length}`); | ||
exports.findOnly = exports.findOnlyIndex = void 0; | ||
function findOnlyIndex(array, predicate, message) { | ||
let foundIndex; | ||
for (let i = 0; i < array.length; i++) { | ||
if (predicate(array[i])) { | ||
if (foundIndex !== undefined) { | ||
throw new Error(message !== null && message !== void 0 ? message : `Expected exactly one item, found more than 1`); | ||
} | ||
foundIndex = i; | ||
} | ||
} | ||
return found[0]; | ||
if (foundIndex === undefined) { | ||
throw new Error(message !== null && message !== void 0 ? message : `Expected exactly one item, found 0`); | ||
} | ||
return foundIndex; | ||
} | ||
exports.findOnlyIndex = findOnlyIndex; | ||
function findOnly(array, predicate, message) { | ||
const index = findOnlyIndex(array, predicate, message); | ||
return array[index]; | ||
} | ||
exports.findOnly = findOnly; |
export * from "./error/caught-result"; | ||
export * from "./array/find"; | ||
export * from "./array/position"; | ||
export * from "./number/format"; |
@@ -19,2 +19,3 @@ "use strict"; | ||
__exportStar(require("./array/find"), exports); | ||
__exportStar(require("./array/position"), exports); | ||
__exportStar(require("./number/format"), exports); |
{ | ||
"name": "@dhau/lang", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "description": "Various language extensions for JS", |
Sorry, the diff of this file is not supported yet
9085
14
166