UNPKG

1.95 kBJavaScriptView Raw
1const minimatch = require('minimatch');
2
3const {getOptions} = require('./config/work-options.js')
4const configs = getOptions()
5const TYPES = configs['types']
6const textGlob = configs['textGlob'];
7
8let types = ['html', 'code'].concat(TYPES)
9let sum = 0;
10
11function getTypeValue(obj,tranArray){
12 sum = 0;
13 /**
14 * @description Find ``obj['type'] === 'value'`` ,and``tranArray.push(obj[key])``
15 * @param {Object} obj
16 * @param {String[]} tranArray
17 * @returns {number} - find value number
18 */
19 function deep(obj, tranArray) {
20 Object.keys(obj).forEach(function (key) {
21
22 // no translate code content
23 if (obj['type'] && (types.some(t => obj['type'] == t))) {
24 return sum
25 }
26 (obj[key] && typeof obj[key] === 'object') && deep(obj[key], tranArray)
27
28
29 if (key === 'value' && obj[key].trim()) {
30 // --text-glob {cli options}
31 if(!textGlob || textGlob.every(g =>minimatch(obj[key],g))){
32 tranArray.push(obj[key])
33 sum++
34 }
35 }
36 });
37 return sum
38 };
39
40 return deep(obj,tranArray)
41}
42function setTypeValue(obj, tranArrayZh){
43 /**
44 * @description Find ``obj['type'] === 'value'``, and use ``tranArrayZh.shift`` set ``obj['value']``
45 * @param {any} obj - AST
46 * @param {String[]} tranArrayZh
47 * @returns
48 */
49 function setdeep(obj, tranArrayZh) {
50 Object.keys(obj).forEach(function (key) {
51
52 if (obj['type'] && (types.some(t => obj['type'] == t))) {
53 return sum
54 }
55
56 (obj[key] && typeof obj[key] === 'object') && setdeep(obj[key], tranArrayZh)
57
58 if (key === 'value' && obj[key].trim()) {
59 if (tranArrayZh.length) {
60 if(!textGlob || textGlob.every(g =>minimatch(obj[key],g))){
61 obj[key] = tranArrayZh.shift()
62 sum--
63 }
64 }
65 }
66 });
67 return sum
68 };
69
70 return setdeep(obj, tranArrayZh)
71}
72module.exports = {
73 getTypeValue,
74 setTypeValue
75}