diff --git a/README.md b/README.md index 4b86b7e..d387d31 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/examples/ignore-keys/.eslintrc.js b/examples/ignore-keys/.eslintrc.js index cb6f29b..02997ac 100644 --- a/examples/ignore-keys/.eslintrc.js +++ b/examples/ignore-keys/.eslintrc.js @@ -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: { diff --git a/package.json b/package.json index aa90f4d..f488d0a 100644 --- a/package.json +++ b/package.json @@ -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" } diff --git a/src/util/deep-for-own.js b/src/util/deep-for-own.js index 3fa16a0..359a388 100644 --- a/src/util/deep-for-own.js +++ b/src/util/deep-for-own.js @@ -1,4 +1,5 @@ const isPlainObject = require('lodash.isplainobject'); +const micromatch = require('micromatch'); /* deep level order traversal. @@ -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, { diff --git a/src/util/deep-for-own.test.js b/src/util/deep-for-own.test.js index 6609c29..4e4f4f6 100644 --- a/src/util/deep-for-own.test.js +++ b/src/util/deep-for-own.test.js @@ -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']); + }); });