README
nv-string-stream
- nv-string-stream
install
- npm install nv-string-stream
usage
example
const str$ = require("nv-string-stream")
gen code
var s = "~🚢~"
> var g = sync_gen_pos_from_str(s)
> Array.from(g)
[ 0, 1, 3 ]
>
> var g = sync_gen_code_point_from_str(s)
> Array.from(g)
[ 126, 128674, 126 ]
>
> var s = "~🚢~"
> get_next_pos(s,0)
1
> get_next_pos(s,1)
3
> get_next_pos(s,3)
4
> get_next_pos(s,4)
undefined
> s.codePointAt(3)
126
> s.codePointAt(1)
128674
> s.codePointAt(0)
126
> s.codePointAt(1)
128674
> s.codePointAt(3)
126
> s.codePointAt(4)
>
var s = "~🚢~"
( async ()=> { let ag = async_gen_pos_from_str(s) for await(let pos of ag) { console.log(pos) } })()
( async ()=> { let ag = async_gen_code_point_from_str(s) for await(let pos of ag) { console.log(pos) } })()
directly gen char
var s = "12345"
var g = str$.sync_gen_from_str(s)
> g.next()
{ value: '1', done: false }
> g.next()
{ value: '2', done: false }
> g.next()
{ value: '3', done: false }
> g.next()
{ value: '4', done: false }
> g.next()
{ value: '5', done: false }
> g.next()
{ value: undefined, done: true }
>
//async generator from string normally USELESS ,just for code compatible
var g = str$.async_gen_from_str(s)
var p = (async () => {
for await (let value of g) {
console.log(value); // 1,2,3,4,5
}
})();
> 1
2
3
4
5
var g = str$.sync_gen_from_file("package.json")
> g.next()
{ value: '{', done: false }
> g.next()
{ value: '\n', done: false }
>
var g = str$.async_gen_from_file("package.json")
var p = (async () => {
for await (let value of g) {
console.log(value); // 1,2,3,4,5
}
})();
/*
> {
"
d
e
p
.....
*/
var g = str$.async_gen_from_file("package.json")
var p = (async () => {
let arr =await str$.async_iter_to_array(g);
console.log(arr)
})();
/*
> [
'{', '\n', ' ', ' ', '"', 'd', 'e', 'p', 'e', 'n', 'd',
'e', 'n', 'c', 'i', 'e', 's', '"', ':', ' ', '{', '\n',
' ', ' ', ' ', ' ', '"', 'n', 'v', '-', 'f', 'a', 'c',
'u', 't', 'i', 'l', '-', 'b', 'a', 's', 'i', 'c', '"',
':', ' ', '"', '^', '1', '.', '0', '.', '1', '1', '"',
'\n', ' ', ' ', '}', ',', '\n', ' ', ' ', '"', 'n', 'a',
'm', 'e', '"', ':', ' ', '"', 'n', 'v', '-', 's', 't',
'r', 'i', 'n', 'g', '-', 's', 't', 'r', 'e', 'a', 'm',
'"', ',', '\n', ' ', ' ', '"', 'd', 'e', 's', 'c', 'r',
'i',
... 222 more items
]
*/
API
char or str
async_gen_from_file(fn,encoding='utf8')
async_gen_chunk_from_file(fn,encoding='utf8',max_chunk_size=4096)
async_gen_from_str(s)
sync_gen_from_file(fn,encoding='utf8')
sync_gen_from_str(s)
async_iter_to_array(async_g)
pos or codepoint
- get_next_pos(s,curr_pos);
- sync_gen_pos_from_str(s);
- sync_gen_code_point_from_str(s);
- async_gen_pos_from_str(s);
- async_gen_code_point_from_str(s);
LICENSE
- ISC