Skip to content

Commit

Permalink
utils: remove always true parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
XhmikosR authored and MohammadYounes committed Feb 8, 2022
1 parent 81c749e commit 20203c4
Showing 1 changed file with 16 additions and 32 deletions.
48 changes: 16 additions & 32 deletions lib/util.js
Expand Up @@ -159,60 +159,44 @@ module.exports = {
restoreTokens (state) {
return this.restore(state)
},
guard (what, who, indexed) {
guard (what, who) {
const state = {
value: who,
store: [],
offset: tokenId++,
token: CHAR_TOKEN_START + tokenId,
indexed: indexed === true
token: CHAR_TOKEN_START + tokenId
}
if (state.indexed === true) {
while (what.test(state.value)) {
state.value = state.value.replace(what, (m) => {
state.store.push(m)
return `${state.token}:${state.store.length}${CHAR_TOKEN_END}`
})
}
} else {

while (what.test(state.value)) {
state.value = state.value.replace(what, (m) => {
state.store.push(m)
return state.token + CHAR_TOKEN_END
return `${state.token}:${state.store.length}${CHAR_TOKEN_END}`
})
}

return state
},
unguard (state, callback) {
if (state.indexed === true) {
const detokenizer = new RegExp('(\\w*?)' + state.token + ':(\\d+)' + CHAR_TOKEN_END, 'i')
while (detokenizer.test(state.value)) {
state.value = state.value.replace(detokenizer, (match, name, index) => {
const value = state.store[index - 1]
return typeof callback === 'function'
? name + callback(value, name)
: name + value
})
}

return state.value
const detokenizer = new RegExp('(\\w*?)' + state.token + ':(\\d+)' + CHAR_TOKEN_END, 'i')
while (detokenizer.test(state.value)) {
state.value = state.value.replace(detokenizer, (match, name, index) => {
const value = state.store[index - 1]
return typeof callback === 'function'
? name + callback(value, name)
: name + value
})
}

return state.value.replace(new RegExp('(\\w*?)' + state.token + CHAR_TOKEN_END, 'i'), (match, name) => {
const value = state.store.shift()
return typeof callback === 'function'
? name + callback(value, name)
: name + value
})
return state.value
},
guardHexColors (value) {
return this.guard(REGEX_HEX_COLOR, value, true)
return this.guard(REGEX_HEX_COLOR, value)
},
unguardHexColors (state, callback) {
return this.unguard(state, callback)
},
guardFunctions (value) {
return this.guard(REGEX_FUNCTION, value, true)
return this.guard(REGEX_FUNCTION, value)
},
unguardFunctions (state, callback) {
return this.unguard(state, callback)
Expand Down

0 comments on commit 20203c4

Please sign in to comment.