Skip to content

yuanfux/zero-width-lib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

zero-width-lib

Travis (.org) npm npm

What's zero-width-lib

Zero-width-lib is a library for manipulating zero width characters (ZWC), which are non-printing and invisible chars.

The common usage of ZWC includes fingerprinting confidential text, embedding hidden text and escaping from string matching (i.e. regex)...

The lib is inspired by this great medium article and got the following features:

  1. 💯stable & cover full test cases
  2. 😆support full width Unicode chars
  3. ⚡️dependencies & performance considered
  4. 📦support CJS, ESM and UMD

Install

npm install zero-width-lib

Usage

Besides ESM, CJS and UMD ways of importing are also supported

// import one method at a time
import { encode } from 'zero-width-lib';
// or import all methods from lib
import * as z from 'zero-width-lib';
// note * represents the invisible ZWC
// U+ represents the Unicode for the character

// 0. six different zwc
const dict = z.zeroWidthDict;
console.log(dict.zeroWidthSpace); // '*' U+200B
console.log(dict.zeroWidthNonJoiner); // '*' U+200C
console.log(dict.zeroWidthJoiner); // '*' U+200D
console.log(dict.leftToRightMark); // '*' U+200E
console.log(dict.rightToLeftMark); // '*' U+200F
console.log(dict.zeroWidthNoBreakSpace); // '*' U+FEFF

// 1. convert text
const text = 'text';
const zwc = z.t2z(text); // '********'
const back = z.z2t(zwc); // 'text'

// 2. embed hidden text
const visble = 'hello world';
const hidden = 'inspired by @umpox';
const encoded = z.encode(visible, hidden); // 'h*********ello world'
const decoded = z.decode(encoded); // 'inpired by @umpox'

// 3. extract ZWC from text
const extracted = z.extract(encoded);
const vis = extracted.vis; // 'hello world'
const hid = extracted.hid; // '*********'

// 4. escape from string matching
const forbidden = 'forbidden';
const escaped = z.split(forbidden); // 'f*o*r*b*i*d*d*e*n*' 

License

MIT

Releases

No releases published

Packages

No packages published