Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/frouriojs/wcslice
https://github.com/frouriojs/wcslice
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/frouriojs/wcslice
- Owner: frouriojs
- Created: 2022-03-10T16:55:19.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-03-01T22:17:45.000Z (almost 2 years ago)
- Last Synced: 2024-11-09T07:13:16.304Z (about 2 months ago)
- Language: TypeScript
- Size: 83 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# wcslice
String `slice` for wide character sensitive.
```sh
npm i wcslice
```Wide characters are counted by [`wcwidth`](https://www.npmjs.com/package/wcwidth).
## Usage
```ts
import wcslice from 'wcslice'
// wcslice(str, [start, [end]])
console.log(wcslice('abc', 0, 1)) // a
console.log(wcslice('aあc', 0, 2)) // a
console.log(wcslice('aあc', 0, 3)) // aあ
console.log(wcslice('aあc', 1, 4)) // あc
console.log(wcslice('aあc', 2, 4)) // c
```Zero-length chars are treated specially. They are treated as infinitesimal in last positions. You can think last zero-length chars as 0.00...001 size.
Float values are supported.```ts
console.log(wcslice('\x00', 0)) // "\x00"
console.log(wcslice('\x00', Number.EPSILON)) // ""
console.log(wcslice('\x00', 0.1)) // ""
console.log(wcslice('\x00', 1)) // ""
```Note that negative numbers are treated as negative positions because of zero-length chars. This differs from `slice`.
```ts
console.log(wcslice('abc', -1)) // abc
```