https://github.com/yuanfux/zero-width-lib
:zero: A lib for zero width chars
https://github.com/yuanfux/zero-width-lib
Last synced: about 2 months ago
JSON representation
:zero: A lib for zero width chars
- Host: GitHub
- URL: https://github.com/yuanfux/zero-width-lib
- Owner: yuanfux
- Created: 2018-08-16T04:06:27.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-03T15:16:23.000Z (about 3 years ago)
- Last Synced: 2026-01-18T05:28:49.803Z (about 2 months ago)
- Language: JavaScript
- Homepage: https://yuanfux.github.io/zero-width-web/
- Size: 288 KB
- Stars: 226
- Watchers: 2
- Forks: 26
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-bookmarks - **zero-width-lib**
README
# zero-width-lib
[](https://img.shields.io/travis/yuanfux/zero-width-lib.svg)
[](https://img.shields.io/npm/v/zero-width-lib.svg)
[]( https://img.shields.io/npm/dt/zero-width-lib.svg)
## 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](https://medium.com/@umpox/be-careful-what-you-copy-invisibly-inserting-usernames-into-text-with-zero-width-characters-18b4e6f17b66) 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
```javascript
// import one method at a time
import { encode } from 'zero-width-lib';
```
```javascript
// or import all methods from lib
import * as z from 'zero-width-lib';
```
```javascript
// 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