ts-vista
Version:
A utility to enhance TypeScript
76 lines (72 loc) • 1.25 kB
JavaScript
const split = (object, keys) => {
const within = {};
const without = {};
for (const key in object) if (keys.includes(key)) within[key] = object[key];
else without[key] = object[key];
return {
within,
without
};
};
/**
* Construct an object with the properties of `object`
* except for those in `keys`.
*
* ### Example
*
* ```ts
* import type { Omit } from "ts-vista";
*
* import { omit } from "ts-vista";
*
* type Keys = {
* a: number;
* b: number;
* c: number;
* };
*
* type Keys2 = Omit<Keys, "a" | "b">;
*
* const keys: Keys = {
* a: 1,
* b: 2,
* c: 3,
* };
*
* const keys2: Keys2 = omit(keys, ["a", "b"]);
* ```
*/
const omit = (object, keys) => {
return split(object, keys).without;
};
/**
* From `object`, pick a set of properties whose keys are in `keys`.
*
* ### Example
*
* ```ts
* import { pick } from "ts-vista";
*
* type Keys = {
* a: number;
* b: number;
* c: number;
* };
*
* type Keys2 = Pick<Keys, "a" | "b">;
*
* const keys: Keys = {
* a: 1,
* b: 2,
* c: 3,
* };
*
* const keys2: Keys2 = pick(keys, ["a", "b"]);
* ```
*/
const pick = (object, keys) => {
return split(object, keys).within;
};
exports.omit = omit;
exports.pick = pick;
//# sourceMappingURL=index.js.map