-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
133 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,28 @@ | ||
export const pickKeys = (obj, ...keys) => { | ||
const result = {} | ||
for (const [key, value] of Object.entries(obj)) { | ||
if (keys.includes(key)) { | ||
result[key] = value | ||
} | ||
return Object.entries(obj).reduce((acc, [key, value]) => { | ||
if (keys.includes(key)) { | ||
acc[key] = value | ||
} | ||
return result | ||
return acc | ||
}, {}) | ||
} | ||
|
||
export const pickLanguages = (obj, ...languages) => { | ||
const result = {} | ||
for (const [key, value] of Object.entries(obj)) { | ||
if (!isObject(value)) { | ||
result[key] = value | ||
continue | ||
} | ||
const filteredChild = {} | ||
for (const [language, childValue] of Object.entries(value)) { | ||
if (languages.includes(language)) { | ||
filteredChild[language] = childValue | ||
} | ||
return Object.entries(obj).reduce((acc, [key, value]) => { | ||
if (!isObject(value)) { | ||
acc[key] = value | ||
} else { | ||
acc[key] = Object.entries(value).reduce((acc, [language, childValue]) => { | ||
if (languages.includes(language)) { | ||
acc[language] = childValue | ||
} | ||
result[key] = filteredChild | ||
return acc | ||
}, {}) | ||
} | ||
return result | ||
return acc | ||
}, {}) | ||
} | ||
|
||
function isObject (x) { | ||
return Object.prototype.toString.call(x) === '[object Object]' | ||
return Object.prototype.toString.call(x) === '[object Object]' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,66 @@ | ||
#!/usr/bin/env node | ||
|
||
import assert from 'assert' | ||
import assert from 'node:assert' | ||
import test from 'node:test' | ||
|
||
import { pickKeys, pickLanguages } from './../lib/util.js' | ||
|
||
test('util.pickKeys', async t => { | ||
await t.test('empty', t => { | ||
assert.deepStrictEqual(pickKeys({}), {}) | ||
}) | ||
await t.test('picks keys', t => { | ||
const result = pickKeys({ | ||
foo: 'bar', | ||
baz: { | ||
foo: 'bar', | ||
}, | ||
qux: 12 | ||
}, 'baz', 'qux') | ||
await t.test('empty', t => { | ||
assert.deepStrictEqual(pickKeys({}), {}) | ||
}) | ||
await t.test('picks keys', t => { | ||
const result = pickKeys({ | ||
foo: 'bar', | ||
baz: { | ||
foo: 'bar' | ||
}, | ||
qux: 12 | ||
}, 'baz', 'qux') | ||
|
||
assert.deepStrictEqual(result, { | ||
baz: { | ||
foo: 'bar' | ||
}, | ||
qux: 12 | ||
}) | ||
assert.deepStrictEqual(result, { | ||
baz: { | ||
foo: 'bar' | ||
}, | ||
qux: 12 | ||
}) | ||
}) | ||
}) | ||
|
||
test('util.pickLanguages', async t => { | ||
await t.test('empty', t => { | ||
assert.deepStrictEqual(pickLanguages({}), {}) | ||
}) | ||
await t.test('empty', t => { | ||
assert.deepStrictEqual(pickLanguages({}), {}) | ||
}) | ||
|
||
await t.test('skip languages', t => { | ||
const result = pickLanguages({ | ||
labels: { | ||
en: { | ||
language: 'en', | ||
value: 'pipe', | ||
}, | ||
fr: { | ||
language: 'fr', | ||
value: 'pipe', | ||
}, | ||
de: { | ||
language: 'de', | ||
value: 'Pfeife', | ||
} | ||
} | ||
}, 'en', 'fr') | ||
await t.test('skip languages', t => { | ||
const result = pickLanguages({ | ||
labels: { | ||
en: { | ||
language: 'en', | ||
value: 'pipe' | ||
}, | ||
fr: { | ||
language: 'fr', | ||
value: 'pipe' | ||
}, | ||
de: { | ||
language: 'de', | ||
value: 'Pfeife' | ||
} | ||
} | ||
}, 'en', 'fr') | ||
|
||
assert.deepStrictEqual(result, { | ||
labels: { | ||
en: { | ||
language: 'en', | ||
value: 'pipe', | ||
}, | ||
fr: { | ||
language: 'fr', | ||
value: 'pipe', | ||
} | ||
} | ||
}) | ||
assert.deepStrictEqual(result, { | ||
labels: { | ||
en: { | ||
language: 'en', | ||
value: 'pipe' | ||
}, | ||
fr: { | ||
language: 'fr', | ||
value: 'pipe' | ||
} | ||
} | ||
}) | ||
}) | ||
}) |