Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using glob ignore patterns #34

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,21 @@ simple
### i18n-json/ignore-keys

- list of key paths (case sensitive) to ignore when checking syntax and doing key structure comparisons. [Example](examples/ignore-keys/)
- you can also use glob patterns (this package uses [micromatch](https://github.com/micromatch/micromatch))
- e.g. if the pattern `*.inProgress` was added to the `ignore-keys` list, then all of the `inProgress`keys below will be ignored.
```json
{
"a": {
"inProgress": "translation"
},
"b": {
"inProgress": "translation"
},
"c": {
"inProgress": "translation"
}
}
```
- this setting is used by the following rules: `i18n-json/identical-keys` and `i18n-json/valid-syntax`
- if the key path points to an object, the nested paths are also ignored.
- e.g. if the key `a` was added to the `ignore-keys` list, then `a.b` will also be ignored.
Expand Down
2 changes: 1 addition & 1 deletion examples/ignore-keys/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = {
*/
'i18n-json/ignore-keys': [
'translationMetadata',
'login.form.inProgressTranslationKey'
'*.inProgressTranslationKey' // No key path ending in `inProgressTranslationKey` will be checked.
]
},
rules: {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"lodash.isplainobject": "^4.0.6",
"lodash.set": "^4.3.2",
"log-symbols": "^2.2.0",
"micromatch": "^3.0.0",
"plur": "^2.1.2",
"pretty-format": "^22.0.3"
}
Expand Down
3 changes: 2 additions & 1 deletion src/util/deep-for-own.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const isPlainObject = require('lodash.isplainobject');
const micromatch = require('micromatch');

/*
deep level order traversal.
Expand All @@ -9,7 +10,7 @@ const isPlainObject = require('lodash.isplainobject');
// calls iteratee with the path to the object.
*/

const shouldIgnorePath = (ignoreList, keyPath) => ignoreList.includes(keyPath.join('.'));
const shouldIgnorePath = (ignoreList, keyPath) => micromatch.isMatch(keyPath.join('.'), ignoreList);
const defaultTraversal = obj => Object.keys(obj);

const deepForOwn = (obj, iteratee, {
Expand Down
28 changes: 28 additions & 0 deletions src/util/deep-for-own.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,32 @@ describe('deepForOwn', () => {
});
expect(visited).toEqual(['a', 'd', 'j', 'e']);
});
it('will not traverse ignored paths using glob', () => {
const obj = {
a: {
b: {
c: 'value'
}
},
d: {
e: {
f: 'value'
}
},
g: {
h: {
i: 'value'
}
},
j: 'value'
};
const visited = [];
deepForOwn(obj, (value, key) => {
visited.push(key);
return true;
}, {
ignorePaths: ['*.b', 'd.*.f', 'g.*']
});
expect(visited).toEqual(['a', 'd', 'g', 'j', 'e']);
});
});