mirror of
https://github.com/robonen/tools.git
synced 2026-03-20 19:04:46 +00:00
refactor: change separate tools by category
This commit is contained in:
2
core/stdlib/src/objects/index.ts
Normal file
2
core/stdlib/src/objects/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './omit';
|
||||
export * from './pick';
|
||||
50
core/stdlib/src/objects/omit/index.test.ts
Normal file
50
core/stdlib/src/objects/omit/index.test.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { omit } from '.';
|
||||
|
||||
describe('omit', () => {
|
||||
it('omit a single key from the object', () => {
|
||||
const result = omit({ a: 1, b: 2, c: 3 }, 'a');
|
||||
|
||||
expect(result).toEqual({ b: 2, c: 3 });
|
||||
});
|
||||
|
||||
it('omit multiple keys from the object', () => {
|
||||
const result = omit({ a: 1, b: 2, c: 3 }, ['a', 'b']);
|
||||
|
||||
expect(result).toEqual({ c: 3 });
|
||||
});
|
||||
|
||||
it('return the same object if no keys are omitted', () => {
|
||||
const result = omit({ a: 1, b: 2, c: 3 }, []);
|
||||
|
||||
expect(result).toEqual({ a: 1, b: 2, c: 3 });
|
||||
});
|
||||
|
||||
it('not modify the original object', () => {
|
||||
const obj = { a: 1, b: 2, c: 3 };
|
||||
const result = omit(obj, 'a');
|
||||
|
||||
expect(obj).toEqual({ a: 1, b: 2, c: 3 });
|
||||
expect(result).toEqual({ b: 2, c: 3 });
|
||||
});
|
||||
|
||||
it('handle an empty object', () => {
|
||||
const result = omit({}, 'a' as any);
|
||||
|
||||
expect(result).toEqual({});
|
||||
});
|
||||
|
||||
it('handle non-existent keys gracefully', () => {
|
||||
const result = omit({ a: 1, b: 2, c: 3 } as Record<string, number>, 'd');
|
||||
|
||||
expect(result).toEqual({ a: 1, b: 2, c: 3 });
|
||||
});
|
||||
|
||||
it('handle null gracefully', () => {
|
||||
const emptyTarget = omit(null as any, 'a');
|
||||
const emptyKeys = omit({ a: 1 }, null as any);
|
||||
|
||||
expect(emptyTarget).toEqual({});
|
||||
expect(emptyKeys).toEqual({ a: 1 });
|
||||
});
|
||||
});
|
||||
38
core/stdlib/src/objects/omit/index.ts
Normal file
38
core/stdlib/src/objects/omit/index.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { isArray, type Arrayable } from '../../types';
|
||||
|
||||
/**
|
||||
* @name omit
|
||||
* @category Objects
|
||||
* @description Returns a new object with the specified keys omitted
|
||||
*
|
||||
* @param {object} target - The object to omit keys from
|
||||
* @param {Arrayable<keyof Target>} keys - The keys to omit
|
||||
* @returns {Omit<Target, Key>} The new object with the specified keys omitted
|
||||
*
|
||||
* @example
|
||||
* omit({ a: 1, b: 2, c: 3 }, 'a') // => { b: 2, c: 3 }
|
||||
*
|
||||
* @example
|
||||
* omit({ a: 1, b: 2, c: 3 }, ['a', 'b']) // => { c: 3 }
|
||||
*
|
||||
* @since 0.0.3
|
||||
*/
|
||||
export function omit<Target extends object, Key extends keyof Target>(
|
||||
target: Target,
|
||||
keys: Arrayable<Key>
|
||||
): Omit<Target, Key> {
|
||||
const result = { ...target };
|
||||
|
||||
if (!target || !keys)
|
||||
return result;
|
||||
|
||||
if (isArray(keys)) {
|
||||
for (const key of keys) {
|
||||
delete result[key];
|
||||
}
|
||||
} else {
|
||||
delete result[keys];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
36
core/stdlib/src/objects/pick/index.test.ts
Normal file
36
core/stdlib/src/objects/pick/index.test.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { pick } from '.';
|
||||
|
||||
describe('pick', () => {
|
||||
it('pick a single key', () => {
|
||||
const result = pick({ a: 1, b: 2, c: 3 }, 'a');
|
||||
|
||||
expect(result).toEqual({ a: 1 });
|
||||
});
|
||||
|
||||
it('pick multiple keys', () => {
|
||||
const result = pick({ a: 1, b: 2, c: 3 }, ['a', 'b']);
|
||||
|
||||
expect(result).toEqual({ a: 1, b: 2 });
|
||||
});
|
||||
|
||||
it('return an empty object when no keys are provided', () => {
|
||||
const result = pick({ a: 1, b: 2 }, []);
|
||||
|
||||
expect(result).toEqual({});
|
||||
});
|
||||
|
||||
it('handle non-existent keys by setting them to undefined', () => {
|
||||
const result = pick({ a: 1, b: 2 }, ['a', 'c'] as any);
|
||||
|
||||
expect(result).toEqual({ a: 1, c: undefined });
|
||||
});
|
||||
|
||||
it('return an empty object if target is null or undefined', () => {
|
||||
const emptyTarget = pick(null as any, 'a');
|
||||
const emptyKeys = pick({ a: 1 }, null as any);
|
||||
|
||||
expect(emptyTarget).toEqual({});
|
||||
expect(emptyKeys).toEqual({});
|
||||
});
|
||||
});
|
||||
38
core/stdlib/src/objects/pick/index.ts
Normal file
38
core/stdlib/src/objects/pick/index.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { isArray, type Arrayable } from '../../types';
|
||||
|
||||
/**
|
||||
* @name pick
|
||||
* @category Objects
|
||||
* @description Returns a partial copy of an object containing only the keys specified
|
||||
*
|
||||
* @param {object} target - The object to pick keys from
|
||||
* @param {Arrayable<keyof Target>} keys - The keys to pick
|
||||
* @returns {Pick<Target, Key>} The new object with the specified keys picked
|
||||
*
|
||||
* @example
|
||||
* pick({ a: 1, b: 2, c: 3 }, 'a') // => { a: 1 }
|
||||
*
|
||||
* @example
|
||||
* pick({ a: 1, b: 2, c: 3 }, ['a', 'b']) // => { a: 1, b: 2 }
|
||||
*
|
||||
* @since 0.0.3
|
||||
*/
|
||||
export function pick<Target extends object, Key extends keyof Target>(
|
||||
target: Target,
|
||||
keys: Arrayable<Key>
|
||||
): Pick<Target, Key> {
|
||||
const result = {} as Pick<Target, Key>;
|
||||
|
||||
if (!target || !keys)
|
||||
return result;
|
||||
|
||||
if (isArray(keys)) {
|
||||
for (const key of keys) {
|
||||
result[key] = target[key];
|
||||
}
|
||||
} else {
|
||||
result[keys] = target[keys];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user