jorel
Version:
A unified wrapper for working with LLMs from multiple providers, including streams, images, documents & automatic tool use.
50 lines (49 loc) • 2.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeById = exports.getById = exports.replaceById = exports.containsId = exports.unEqualsId = exports.equalsId = exports.extractIds = exports.extractId = exports.lastEntry = exports.firstEntry = void 0;
exports.getUniqueIds = getUniqueIds;
/** Get the first value in an array */
const firstEntry = (values) => (values.length === 0 ? null : values[0]);
exports.firstEntry = firstEntry;
/** Get the last value in an array */
const lastEntry = (values) => (values.length === 0 ? null : values[values.length - 1]);
exports.lastEntry = lastEntry;
/** Extract the id from an object (or string) */
const extractId = (idOrValue1) => typeof idOrValue1 === "string" ? idOrValue1 : idOrValue1.id;
exports.extractId = extractId;
/** Extract the ids from a list of objects */
const extractIds = (values) => values.map(exports.extractId);
exports.extractIds = extractIds;
function getUniqueIds(values, asSet = false) {
const result = new Set();
values.forEach((v) => result.add((0, exports.extractId)(v)));
return asSet ? result : Array.from(result.values());
}
/** Checks whether the objects (or strings) share the same id */
const equalsId = (idOrValue1) => (idOrValue2) => (0, exports.extractId)(idOrValue1) === (0, exports.extractId)(idOrValue2);
exports.equalsId = equalsId;
/** Checks whether the objects (or strings) do not share the same id */
const unEqualsId = (idOrValue1) => (idOrValue2) => (0, exports.extractId)(idOrValue1) !== (0, exports.extractId)(idOrValue2);
exports.unEqualsId = unEqualsId;
/** Checks whether the list of objects (or strings) contains the given id */
const containsId = (values, idOrValue) => values.some((0, exports.equalsId)(idOrValue));
exports.containsId = containsId;
/** Replaces an object by its id */
const replaceById = (values, replacementValue, addIfNotFound = false) => {
if ((0, exports.containsId)(values, replacementValue)) {
return values.map((v) => (v.id === replacementValue.id ? replacementValue : v));
}
else {
if (addIfNotFound)
return [...values, replacementValue];
else
return values;
}
};
exports.replaceById = replaceById;
/** Retrieves an object by its id */
const getById = (values, valueOrId) => values.find((0, exports.equalsId)(valueOrId)) ?? null;
exports.getById = getById;
/** Remove an object by its id */
const removeById = (values, valueOrId) => values.filter((0, exports.unEqualsId)(valueOrId));
exports.removeById = removeById;