-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #189 from stylify/mangling
Mangling
- Loading branch information
Showing
7 changed files
with
179 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './objects'; | ||
export * from './numbers'; | ||
export * from './strings'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
export const dollarPlaceholder = '_DLR_'; | ||
|
||
export const backslashPlaceholder = '_BSLASH_'; | ||
|
||
export type TokenizerCallbackType = (data: { | ||
iterator: number, | ||
token: string|undefined, | ||
nextToken: string|undefined, | ||
previousToken: string|undefined, | ||
tokensCount: number, | ||
isLastToken: boolean, | ||
isFirstToken: boolean | ||
}) => boolean|void; | ||
|
||
export const escapeCssSelector = (selector: string, all = false) => { | ||
const selectorLength = selector.length; | ||
let result = ''; | ||
const regExp = all ? /[^a-zA-Z0-9]/ : /[.*+?^${}()|[\]\\]/; | ||
|
||
for (let i = 0; i < selectorLength; i++) { | ||
const char = selector[i]; | ||
const escapeCharacter = '\\'; | ||
|
||
if (['-', '_'].includes(char) || !regExp.test(char) || selector[i - 1] === escapeCharacter) { | ||
result += char; | ||
continue; | ||
} | ||
|
||
result += `\\${char}`; | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
export const tokenize = (content: string, tokenizerCallback: TokenizerCallbackType): void => { | ||
const tokens = content.split(''); | ||
const tokensCount = tokens.length; | ||
|
||
for (let i = 0; i < tokensCount; i ++) { | ||
if (tokenizerCallback({ | ||
token: tokens[i], | ||
nextToken: tokens[i + 1], | ||
previousToken: tokens[i - 1], | ||
iterator: i, | ||
isLastToken: i + 1 === tokensCount, | ||
isFirstToken: i === 0, | ||
tokensCount | ||
})) { | ||
break; | ||
} | ||
} | ||
}; | ||
|
||
const dollarPlaceholderRegExp = new RegExp(dollarPlaceholder, 'g'); | ||
const backslashPlaceholderRegExp = new RegExp(backslashPlaceholder, 'g'); | ||
|
||
export const prepareStringForReplace = (content: string) => { | ||
|
||
/* | ||
This replaces special characters used within regular expression | ||
so their are not processed during replacing | ||
$ => because $$ causes $ | ||
\ => because \u or \v for example are predefined character sets | ||
*/ | ||
return content.replace(/\$/g, dollarPlaceholder).replace(/\\/g, backslashPlaceholder); | ||
}; | ||
|
||
export const getStringOriginalStateAfterReplace = (content: string) => { | ||
return content.replace(dollarPlaceholderRegExp, '$$').replace(backslashPlaceholderRegExp, '\\'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.