-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathfilter.js
More file actions
79 lines (74 loc) · 2.02 KB
/
Copy pathfilter.js
File metadata and controls
79 lines (74 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
'use strict';
/**
* @module paper/lib/translator/filter
*
* This should be considered an internal concern of Translator, and not a
* public interface.
*/
/*
* Internal method to filter an object containing string keys using the given keyPrefix
*
* @private
* @param {Object.<string, string>} obj
* @param {string} keyPrefix
* @returns {Object.<string, string>}
*/
function filterByKeyPrefix(obj, keyPrefix) {
const result = {};
for (const key in obj) {
if (typeof key === 'string' && key.startsWith(keyPrefix)) {
result[key] = obj[key];
}
}
return result;
}
/**
* Filter translation and locales of the given language object by translation key prefix.
* This is used by the `langJson` helper. This method expects an object in the format
* returned by `translator/transformer.js:transform()`.
*
* The incoming language object looks like this:
* {
* locale: 'en',
* locales: {
* 'salutations.welcome': 'en',
* 'salutations.hello': 'en',
* 'salutations.bye': 'en',
* items: 'en',
* },
* translations: {
* 'salutations.welcome': 'Welcome',
* 'salutations.hello': 'Hello {name}',
* 'salutations.bye': 'Bye bye',
* items: '{count, plural, one{1 Item} other{# Items}}',
* }
* }
*
* The return value, assuming `keyFilter` of `salutations`, would look like this:
* {
* locale: 'en',
* locales: {
* 'salutations.welcome': 'en',
* 'salutations.hello': 'en',
* 'salutations.bye': 'en',
* },
* translations: {
* 'salutations.welcome': 'Welcome',
* 'salutations.hello': 'Hello {name}',
* 'salutations.bye': 'Bye bye',
* }
* }
* @param {Object.<string, string|Object>} language
* @param {string} keyFilter
* @returns {Object.<string, string|Object>}
*/
function filterLanguageObject(language, keyFilter) {
return {
locale: language.locale,
locales: filterByKeyPrefix(language.locales, keyFilter),
translations: filterByKeyPrefix(language.translations, keyFilter),
};
}
module.exports = {
filterByKey: filterLanguageObject,
};