Skip to content

Data

Filter keys by inclusion

filterByKeyInclusion is a function that filters an object by including only the specified keys.

Include only specific keys
import { filterByKeyInclusion } from '@basalt-lab/basalt-helper/data';

const data = {
    a: 'a',
    b: 'b',
    c: 'c',
    d: 'd',
} as const;

const filteredData = filterByKeyInclusion(data, ['a', 'b', 'c']);
console.log(filteredData);

bun example.ts{ a: 'a', b: 'b', c: 'c' }

Include only specific keys and only if they have a value
import { filterByKeyInclusion } from '@basalt-lab/basalt-helper/data';

const data = {
    a: 'a',
    b: undefined,
    c: null,
    d: 'd',
} as const;

const filteredData = filterByKeyInclusion(data, ['a', 'b', 'c'], true);
console.log(filteredData);

bun example.ts{ a: 'a' }

Filter keys by exclusion

filterByKeyExclusion is a function that filters an object by excluding specified keys.

Exclude specific keys
import { filterByKeyExclusion } from '@basalt-lab/basalt-helper/data';

const data = {
    a: 'a',
    b: 'b',
    c: 'c',
    d: 'd',
} as const;

const filteredData = filterByKeyExclusion(data, ['a', 'b']);
console.log(filteredData);

bun example.ts{ c: 'c', d: 'd' }

Exclude specific keys and those without values
import { filterByKeyExclusion } from '@basalt-lab/basalt-helper/data';

const data = {
    a: 'a',
    b: null,
    c: undefined,
    d: 'd',
} as const;

const filteredData = filterByKeyExclusion(data, ['a'], true);
console.log(filteredData);

bun example.ts{ d: 'd' }

Include keys by pattern

filterByKey is a function that filters an object by including keys according to a pattern.

Include keys by pattern
import { filterByValue } from '@basalt-lab/basalt-helper/data';

const data = {
    a: 'a',
    b: 'b',
    c: 'c',
    d: 'd',
} as const;

const filteredData = filterByValue(data, (value: string): boolean => value === 'a' || value === 'b');
console.log(filteredData);

bun example.ts{ a: 'a', b: 'b' }

Include keys by pattern and only if they have a value
import { filterByValue } from '@basalt-lab/basalt-helper/data';

const data = {
    a: 'a',
    b: undefined,
    c: null,
    d: 'd',
} as const;

const filteredData = filterByValue(data, (value: unknown): boolean => value === 'a', true);
console.log(filteredData);

bun example.ts{ a: 'a' }

Exclude keys by pattern

filterByValue is a function that filters an object by excluding keys according to a pattern.

Exclude keys by pattern
import { filterByValue } from '@basalt-lab/basalt-helper/data';

const data = {
    a: 'a',
    b: 'b',
    c: 'c',
    d: 'd',
} as const;

const filteredData = filterByValue(data, (value: string): boolean => value !== 'a' && value !== 'b');
console.log(filteredData);

bun example.ts{ c: 'c', d: 'd' }

Exclude keys by pattern and those without values
import { filterByValue } from '@basalt-lab/basalt-helper/data';

const data = {
    a: 'a',
    b: null,
    c: undefined,
    d: 'd',
} as const;

const filteredData = filterByValue(data, (value: unknown): boolean => value !== 'a', true);
console.log(filteredData);

bun example.ts{ d: 'd' }

Transform object keys

transformKeys is a function that transforms object keys according to a specified transformer.

Transform object keys to camelCase
import { transformKeys } from '@basalt-lab/basalt-helper/data';
import { BasaltCamelCaseTransformer } from '@basalt-lab/basalt-helper/data/transformer';

const data = {
    'camelCase': undefined,
    'PascalCase': undefined,
    'kebab-case': undefined,
    'snake_case': undefined
};

console.log(transformKeys(data, new BasaltCamelCaseTransformer()));

bun example.ts{
camelCase: undefined,
pascalCase: undefined,
kebabCase: undefined,
snakeCase: undefined
}

Transform object keys to PascalCase
import { transformKeys } from '@basalt-lab/basalt-helper/data';
import { BasaltPascalCaseTransformer } from '@basalt-lab/basalt-helper/data/transformer';

const data = {
    'camelCase': undefined,
    'PascalCase': undefined,
    'kebab-case': undefined,
    'snake_case': undefined
};

console.log(transformKeys(data, new BasaltPascalCaseTransformer()));

bun example.ts{
CamelCase: undefined,
PascalCase: undefined,
KebabCase: undefined,
SnakeCase: undefined
}

Transform object keys to kebab-case
import { transformKeys } from '@basalt-lab/basalt-helper/data';
import { BasaltKebabCaseTransformer } from '@basalt-lab/basalt-helper/data/transformer';

const data = {
    'camelCase': undefined,
    'PascalCase': undefined,
    'kebab-case': undefined,
    'snake_case': undefined
};

console.log(transformKeys(data, new BasaltKebabCaseTransformer()));

bun example.ts{
'camel-case': undefined,
'pascal-case': undefined,
'kebab-case': undefined,
'snake-case': undefined
}

Transform object keys to snake_case
import { transformKeys } from '@basalt-lab/basalt-helper/data';
import { BasaltSnakeCaseTransformer } from '@basalt-lab/basalt-helper/data/transformer';

const data = {
    'camelCase': undefined,
    'PascalCase': undefined,
    'kebab-case': undefined,
    'snake_case': undefined
};

console.log(transformKeys(data, new BasaltSnakeCaseTransformer()));

bun example.ts{
camel_case: undefined,
pascal_case: undefined,
kebab_case: undefined,
snake_case: undefined
}