改变创造价值
用户体验至上
Escape Notation
转义字符
A string literal is zero or more characters enclosed in single or double quotes. Each character may be represented by an escape sequence. All characters may appear literally in a string literal except for the closing quote character, backslash, carriage return, line separator, paragraph separator, and line feed. Any character may appear in the form of an escape sequence.
const zeroWidthSpace = '\u200B' const zeroWidthJoiner = '\u200D' const zeroWidthNonJoiner = '\u200C' const zeroWidthNonBreakSpace = '\uFEFF' function createEncryptionText(text) { if (!text || typeof text !== 'string') { throw new Error('invalid param, param must be string') } const binaryText = textToBinary(text) return binaryText .split('') .map(b => { const num = parseInt(b, 10) if (num === 1) { return zeroWidthSpace } if (num === 0) { return zeroWidthNonJoiner } return zeroWidthJoiner }) .join(zeroWidthNonBreakSpace) } function charToBinary(char) { return char.charCodeAt(0).toString(2) } function textToBinary(text) { return text .split('') .map(item => padStar(charToBinary(item))) .join(' ') } function padStar(text, length = 8, chars = '0') { if (typeof text !== 'string') { throw new Error('invalid params. text must be string') } return ( Array(length) .fill(chars) .slice(text.length) + text ) } console.log(createEncryptionText('wfsovereign')) // "" console.log(createEncryptionText('wfsovereign').length) // 195