Skip to content

Commit 68153e5

Browse files
committed
Add new toArray and toString functions to the objects package
1 parent 60e2236 commit 68153e5

File tree

6 files changed

+50
-3
lines changed

6 files changed

+50
-3
lines changed

src/objects/index.test.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import {
2-
camelizeKeys, dasherizeKeys, underscoreKeys,
32
isObject,
43
removeKeys, removeKeysWithBlankValues,
54
swapKeysAndValues,
5+
toArray,
6+
toString,
7+
camelizeKeys, dasherizeKeys, underscoreKeys,
68
} from '.';
79

810
test('expors', () => {
911
[
10-
camelizeKeys, dasherizeKeys, underscoreKeys,
1112
isObject,
1213
removeKeys, removeKeysWithBlankValues,
1314
swapKeysAndValues,
15+
toArray,
16+
toString,
17+
camelizeKeys, dasherizeKeys, underscoreKeys,
1418
].map(item => expect(item).toBeDefined());
1519
});

src/objects/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
export { camelizeKeys, dasherizeKeys, underscoreKeys } from './transform-keys';
21
export { isObject } from './is-object';
32
export { removeKeys, removeKeysWithBlankValues } from './remove-keys';
43
export { swapKeysAndValues } from './swap-keys-and-values';
4+
export { toArray } from './toArray';
5+
export { toString } from './toString';
6+
export { camelizeKeys, dasherizeKeys, underscoreKeys } from './transform-keys';

src/objects/toArray/index.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { toArray } from '.';
2+
3+
import type { Cases } from 'testing/helpers';
4+
5+
describe('test toArray function', () => {
6+
const cases: Cases<typeof toArray> = [
7+
['normal use case', [{ a: 1, b: 2 }], ['a: 1', 'b: 2']],
8+
['null use case', [{ a: 1, b: null }], ['a: 1']],
9+
['undefined use case', [{ a: 1, b: undefined }], ['a: 1']],
10+
];
11+
test.each(cases)('%s', (_, args, expected) => {
12+
const actual = toArray(...args);
13+
expect(actual).toEqual(expected);
14+
});
15+
});

src/objects/toArray/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { removeKeysWithBlankValues } from '../remove-keys';
2+
3+
export function toArray(object: Record<string, unknown>) {
4+
return Object.keys(removeKeysWithBlankValues(object)).map(key => `${key}: ${object[key]}`);
5+
}

src/objects/toString/index.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { toString } from '.';
2+
3+
import type { Cases } from 'testing/helpers';
4+
5+
describe('test toString function', () => {
6+
const cases: Cases<typeof toString> = [
7+
['normal use case', [{ a: 1, b: 2 }], 'a: 1\nb: 2'],
8+
['null use case', [{ a: 1, b: null }], 'a: 1'],
9+
['undefined use case', [{ a: 1, b: undefined }], 'a: 1'],
10+
['delimite use case', [{ a: 1, b: 2 }, ', '], 'a: 1, b: 2'],
11+
];
12+
test.each(cases)('%s', (_, args, expected) => {
13+
const actual = toString(...args);
14+
expect(actual).toEqual(expected);
15+
});
16+
});

src/objects/toString/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { toArray } from '../toArray';
2+
3+
export function toString(styles: Record<string, unknown>, delimiter = '\n') {
4+
return toArray(styles).join(delimiter);
5+
}

0 commit comments

Comments
 (0)