# nv-string-basic

> nv-string-basic =============== - nv-string-basic

Latest version **1.0.56** (published 2025-12-13) · ISC license · 0 weekly downloads

## Install

```sh
npm install nv-string-basic
pnpm add nv-string-basic
yarn add nv-string-basic
bun add nv-string-basic
```

## Health

**Score 45/100 (D)** — status: stable.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

## Facts

| | |
|---|---|
| Version | 1.0.56 |
| Published | 2025-12-13 |
| First published | 2021-02-27 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 177.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | ihgazni2 |

## Links

- npm: https://www.npmjs.com/package/nv-string-basic
- npm.io page: https://npm.io/package/nv-string-basic

## Dependencies (5)

- [charcodes](https://npm.io/package/charcodes.md) ^0.2.0
- [nv-char-basic](https://npm.io/package/nv-char-basic.md) ^1.0.2
- [nv-array-combo](https://npm.io/package/nv-array-combo.md) ^1.0.0
- [nv-facutil-basic](https://npm.io/package/nv-facutil-basic.md) ^1.3.11
- [nv-char-whitespace](https://npm.io/package/nv-char-whitespace.md) ^1.0.7

## Recent versions

- 1.0.56 (latest) — 2025-12-13
- 1.0.55 — 2025-03-03
- 1.0.54 — 2025-03-03
- 1.0.53 — 2024-07-15
- 1.0.51 — 2022-09-07
- 1.0.50 — 2022-08-26
- 1.0.49 — 2022-08-26
- 1.0.48 — 2022-08-25
- 1.0.47 — 2022-08-22
- 1.0.46 — 2022-06-13
- 1.0.45 — 2022-06-07
- 1.0.43 — 2022-06-03
- 1.0.39 — 2022-06-03
- 1.0.38 — 2022-05-07
- 1.0.37 — 2022-04-25
- … 35 more at https://npm.io/package/nv-string-basic/versions

## README

nv-string-basic
===============
- nv-string-basic    
- some basic string utils
- length ,number 
- reusable-nonnest-string-template

install
=======
- npm install nv-string-basic 

usage
=====
    
example
-------

###to\_str

    const {to_str} = require("nv-string-basic")
    > to_str(undefined)
    'undefined'
    > to_str(null)
    'null'
    > to_str(123)
    '123'
    > to_str(123n)
    '123n'
    > to_str('abc')
    'abc'
    > to_str(Symbol('sym'))
    'sym'
    > console.log(to_str({a:100}))
    {
        "a": 100
    }
    undefined
    > console.log(to_str([100,200,300]))
    [
        100,
        200,
        300
    ]
    undefined
    > class O {
         [util.inspect.custom]() {
             return('custom')
         }
    }
    > var o = new O()
    > to_str(o)
    'custom'
    >
    
    > function tst() {return(999)}
    undefined
    > to_str(tst)
    'function tst() {return(999)}'
    >

### pend string-array

    > var ary = [
    ...   'ready',
    ...   'conding',
    ...   'opened',
    ...   'self_executing',
    ...   'resolved',
    ...   'self_rejected',
    ...   'bubble_rejected',
    ...   'self_paused',
    ...   'bubble_paused',
    ...   'impossible'
    ... ]
    undefined
    > x.ppend_fmt_sary(ary)
    [
      '          ready',
      '        conding',
      '         opened',
      ' self_executing',
      '       resolved',
      '  self_rejected',
      'bubble_rejected',
      '    self_paused',
      '  bubble_paused',
      '     impossible'
    ]
    > x.apend_fmt_sary(ary)
    [
      'ready          ',
      'conding        ',
      'opened         ',
      'self_executing ',
      'resolved       ',
      'self_rejected  ',
      'bubble_rejected',
      'self_paused    ',
      'bubble_paused  ',
      'impossible     '
    ]
    >


### stmpl and dtmpl

    //first compile
    var reuseable = stmpl`${0}-${1}-${2}-${`${3}`}`

    > reuseable('x','y','z','nest2')
    'x-y-z-nest2'
    >


    > reuseable('x','y')
    'x-y--'
    >

    //first compile
    var reuseable = stmpl`
        <${0}>
            <${1}></${1}>
            <${1}></${1}>
            <${1}></${1}>
            <${1}></${1}>
        </${0}>
    `

    var s = reusable('div','span')


    <div>
        <span></span>
        <span></span>
        <span></span>
        <span></span>
    </div>


     
    > console.log(reuseable('head','meta'))

        <head>
            <meta></meta>
            <meta></meta>
            <meta></meta>
            <meta></meta>
        </head>

    undefined
    >


    //
    var reusable = dtmpl`${'b'}-${'i'}-${'a'}-${`${'t'}`}`

    reusable({
        b:'before',
        i:'in',
        a:'after',
        t:'tail'
    })
    
    >
    'before-in-after-tail'
   
    
    var reusable = dtmpl`
        ${'obj_open'}
            ${'ary_open'} 100,200,300,400 ${'ary_close'}
        ${'obj_close'}
    `
    
    var s = reusable({
        obj_open: '【',
        obj_close: '】',
        ary_open:'<',
        ary_close:'>'
    })


    > console.log(s)

            【
                < 100,200,300,400 >
            】

    >


    var s = reusable({
        obj_open: '<div>',
        obj_close: '</div>',
        ary_open:'<ul>',
        ary_close:'</ul>'
    })


    > console.log(s)

            <div>
                <ul> 100,200,300,400 </ul>
            </div>

    >


    var reusable = dtmpl`
        ${'open'}
            ${'open'} ${'close'}
            ${'open'} ${'close'}
            ${'open'} ${'close'}
        ${'close'}
    `
    
    var s = reusable({open:'<div>',close:'</div>'})
    

    > console.log(s)

            <div>
                <div> </div>
                <div> </div>
                <div> </div>
            </div>

    >

    
    var s = reusable({open:'<custom>',close:'</custom>'})


        <custom>
            <custom> </custom>
            <custom> </custom>
            <custom> </custom>
        </custom>



    /*
         because string-literal-template is a runtime feature,
         so stmpl and dtmpl NOT support nest ``,
         to handle nest need to parse the template-source and implement the evaluate ,refer to nv-facutil-nest-string-tem,
         its NOT convinient to use, USELESS,
    */


### num

    > const {prepend_num,append_num} = require("nv-string-basic")
    undefined
    > append_num(5,2,4)
    '1010'
    > prepend_num(5,2,4)
    '0101'
    >


### loose parse\_num


    parse_num("1122334455667788998877665544332211n")
    parse_num("-1122334455667788998877665544332211n")
    
    parse_num("123")
    parse_num("-123")
    parse_num("0x1101")
    parse_num("0b1101")
    parse_num("0o1101")
    
    
    parse_num("123.12")
    parse_num("-123.12")
    parse_num("123.12e3")
    parse_num("-123.12e3")
    
    parse_num("123.12e+3")
    parse_num("-123.12e-3")
    
    
    parse_num("2e0.5")
    parse_num("-2e0.5")
    
    
    > parse_num("1122334455667788998877665544332211n")
    1122334455667788998877665544332211n
    > parse_num("-1122334455667788998877665544332211n")
    -1122334455667788998877665544332211n
    >
    > parse_num("123")
    123
    > parse_num("-123")
    -123
    >
    
    > parse_num("0x1101")
    4353
    > parse_num("0b1101")
    13
    > parse_num("0o1101")
    577
    
    > parse_num("123.12")
    123.12
    > parse_num("-123.12e3")
    -123120
    >
    
    > parse_num("123.12e3")
    123120
    > parse_num("12e2")
    1200
    
    > parse_num("123.12e+3")
    123120
    > parse_num("-123.12e-3")
    -0.12312
    >
    
    >
    > parse_num("2e0.5")
    6.324555320336759       //    2 * (10 ** 0.5)
    >
    > parse_num("-2e0.5")
    -6.324555320336759
    >
    > parse_num("+2e0.5")
    6.324555320336759
    > parse_num("-2e0.5")
    -6.324555320336759
    >


### keep the type infomation 


    > NUM_TYPE_DICT
    {
      '6': 'Integer',
      '7': 'Float',
      '8': 'BigInt',
      '9': 'PosInfinity',
      '10': 'NegInfinity',
      '11': 'NaN',
      Integer: 6,
      Float: 7,
      BigInt: 8,
      PosInfinity: 9,
      NegInfinity: 10,
      NaN: 11
    }
    > parse_num("1122334455667788998877665544332211n",true)
    { value: 1122334455667788998877665544332211n, type: 8 }
    > parse_num("123",true)
    { value: 123, type: 6 }
    > NUM_TYPE_DICT[6]
    'Integer'
    > parse_num("123.",true)
    { value: 123, type: 7 }
    > NUM_TYPE_DICT[7]
    'Float'
    >
    > parse_num("123e2",true)
    { value: 12300, type: 7 }
    > NUM_TYPE_DICT[7]
    'Float'
    >
    > parse_num("Infinity",true)
    { value: Infinity, type: 9 }
    > NUM_TYPE_DICT[9]
    'PosInfinity'
    > parse_num("+Infinity",true)
    { value: Infinity, type: 9 }
    > NUM_TYPE_DICT[9]
    'PosInfinity'
    > parse_num("-Infinity",true)
    { value: -Infinity, type: 10 }
    > NUM_TYPE_DICT[9]
    'PosInfinity'
    > NUM_TYPE_DICT[10]
    'NegInfinity'
    > parse_num("NaN",true)
    { value: NaN, type: 11 }
    > NUM_TYPE_DICT[11]
    'NaN'
    >


### loose parse\_int

    parse_int("0b1101")
    0b1101
    parse_int("0o1101")
    0o1101
    parse_int("0x1101")
    0x1101
    parse_int("1101")

    > parse_int("af") === undefined
    > parse_int("0xaf")
    175
    >


    /*
    > parse_int("0b1101")
    13
    > 0b1101
    13
    > parse_int("0o1101")
    577
    > 0o1101
    577
    > parse_int("0x1101")
    4353
    > 0x1101
    4353
    > parse_int("1101")
    1
    >

    */



### strict parse\_float


    //valid  and same as  parseFloat
    parse_float("1")
    parse_float("-1")
    parse_float(".1")
    parse_float("-.1")
    parse_float("1.")
    parse_float("-1.")
    parse_float("-1.1")
    parse_float("1.1")
    parse_float("-1.1e2")
    parse_float(".1E3")
    /*
    > parse_float("1")
    1
    > parse_float("-1")
    -1
    > parse_float(".1")
    0.1
    > parse_float("-.1")
    -0.1
    > parse_float("1.")
    1
    > parse_float("-1.")
    -1
    > parse_float("-1.1")
    -1.1
    > parse_float("1.1")
    1.1
    >
    > parse_float("-1.1e2")
    -110
    >
    > parse_float(".1E3")
    100
    
    */
    
    //valid  but different with parseFloat
    
    parse_float("1.1e")
    parse_float("-1.1e")
    parse_float("-1.1e2.5")
    parse_float(".1E3.")
    parse_float(".1E3.13")
    /*
    > parse_float("1.1e")
    1.1
    > parse_float("-1.1e")
    -1.1
    > parse_float("-1.1e2.5")
    -347.85054261852173
    >
    > parse_float(".1E3.")
    100
    > parse_float(".1E3.13")
    134.89628825916535
    >
    
    */
    
    //invalid
    parse_float(".") === undefined 
    parse_float("1.2.") === undefined 
    parse_float("-.") === undefined



### parse0  only parse undefined/null/boolean/number 

    > parse0('undefined')
    undefined
    > parse0('null')
    null
    > parse0('true')
    true
    > parse0('false')
    false
    > parse0('123')
    123
    > parse0('123e2')
    12300
    > parse0('Infinity')
    Infinity
    > parse0('-Infinity')
    -Infinity
    > parse0('-NaN')
    NaN
    > parse0('NaN')
    NaN

    > parse0('ssss')
    ssss
    >



reverse
-------

    var s = "abc"
    > reverse(s)
    'cba'
    >

trim
----

    var s = '   \t\r\na b c'
    > trim_left(s)
    'a b c'
    >

    var s = 'abcabc d e f abc'
    > trim_left(s,"abc ")
    'd e f abc'
    >
    > trim_right(s,"abc ")
    'abcabc d e f'
    >
    > trim(s,"abc ")
    'd e f'
    >

    > trim(s,["abcabc"," abc"])
    ' d e f'
    >
    var s = '我你 a b c   它'

    trim(s,(ch)=>ch.codePointAt(0)>256 ||ch.codePointAt(0)===32 )
    > trim(s,(ch)=>ch.codePointAt(0)>256 ||ch.codePointAt(0)===32 )
    'a b c'
    >

    var s = '%$# abc !!@'
    > trim_left(s,/^[^a-z]+/)
    'abc !!@'
    > trim_right(s,/[^a-z]+$/)
    '%$# abc'
    >


gen\_split
----------

    > var g = gen_splits("abcd")
    undefined
    > Array.from(g)
    [
      [ 'abcd' ],
      [ 'a', 'bcd' ],
      [ 'ab', 'cd' ],
      [ 'a', 'b', 'cd' ],
      [ 'abc', 'd' ],
      [ 'a', 'bc', 'd' ],
      [ 'ab', 'c', 'd' ],
      [ 'a', 'b', 'c', 'd' ]
    ]
    > var g = gen_splits("abcd",2)
    undefined
    > Array.from(g)
    [ [ 'a', 'bcd' ], [ 'ab', 'cd' ], [ 'abc', 'd' ] ]
    >

### u-string

    var s = '𝑒';
    > Array.from(s).length;
    1
    > s.length
    2
    >



    > u(s);
    '\\ud835\\udc52'
    >
    > from_u('\\ud835\\udc52')
    '𝑒'
    >



    > u('A-𝑒-B')
    '\\u0041\\u002d\\ud835\\udc52\\u002d\\u0042'
    >
    > from_u('\\u0041\\u002d\\ud835\\udc52\\u002d\\u0042')
    'A-𝑒-B'
    >


    > u('A-𝑒-B',true)
    'A-\\ud835\\udc52-B'
    >
    > from_u('A-\\ud835\\udc52-B')
    'A-𝑒-B'
    >

### try\_dequote, noquote if unnecessary 


        try_dequote(s,q='"',escc=['\b', '\f', '\n', '\r', '\t', '\x0B' ,' ','"','`',"'"])

        > try_dequote('abc')
        'abc'
        > try_dequote('ab c')
        '"ab c"'
        > JSON.stringify('a\u0020bc')
        '"a bc"'
        >
        > try_dequote('a\ud835bc')
        '"a\\ud835bc"'
        >
        > try_dequote('a\xeebc')
        'aîbc'
        >

### "operators"


        //ONLY ADD  is acurate, coz only string-add is monoid
        > add('x','y')
        'xy'
        >

        //OTHERS
        > mul('abc',3)
        'abcabcabc'
        > mul(3,'abc')
        'abcabcabc'
        >

        > sub('ABCxyzDEF','ABC')
        'xyzDEF'
        >
        > sub('ABCxyzDEF','DEF')
        'ABCxyz'
        >


        > length_div('abcdefg','abc')
        [ 2, 1 ]
        >
        > length_quo('abcdefg','abc')
        2
        > length_mod('abcdefg','abc')
        1
        >


        > div('abcabcabcab',3)
        [ [ 'abc', 'abc', 'abc' ], 'ab' ]
        >
        > quo('abcabcabcab',3)
        [ 'abc', 'abc', 'abc' ]
        >
        > mod('abcabcabcab',3)
        'ab'
        >

        > sdiv('abcabcabcab','abc')
        [ 3, 'ab' ]
        >
        > squo('abcabcabcab','abc')
        3
        >
        > smod('abcabcabcab','abc')
        'ab'
        >

        > sdiv('abcabcaXcab','abc')
        [ null, null ]
        >

### match\_by

        > x.match_by('a','a')
        true
        > x.match_by('a',/a+/)
        true
        > x.match_by('a',(s)=>s==='a')
        true
        >


### match

        > match('aaaXFbbxfuuu',/xf/gi)
        [
          [ 'a', 'a', 'a', matched: false ],
          [ 'X', 'F', matched: true ],
          [ 'b', 'b', matched: false ],
          [ 'x', 'f', matched: true ],
          [ 'u', 'u', 'u', matched: false ]
        ]
        >


### sary\_inseq\_matchby  sary\_in\_continuous\_seq\_matchby


        > x.sary_inseq_matchby(["a","abbb","asss","b","as"],[/ab+/,/as+/,/as/])
        true
        > x.sary_inseq_matchby(["a","abbb","asss","b","As"],[/ab+/,/as+/,/as/])
        false


        > x.sary_in_continuous_seq_matchby(["a","abbb","asss","b","as"],[/ab+/,/as+/,/as/])
        false
        > x.sary_in_continuous_seq_matchby(["a","abbb","asss","as","b"],[/ab+/,/as+/,/as/])
        true

### \_char\_at
- without validate

####

        > var s='A𝑒B'
        > _char_at(s,0)
        'A'
        > _char_at(s,1)            //high-surr  of high-low char
        '𝑒'
        > _char_at(s,2)            //low-surr   of high-low char
        '𝑒'
        > _char_at(s,3)
        'B'
        > s.length
        4
        >





API
====

- to\_str(o)

- stmpl`tmpl`
- dtmpl`tmpl`

- prepend(s,width,pad=" ",force=false)
- append(s,width,pad=" ",force=false)

- prepend\_num(n,radix,width,pad="0",force=false)
- append\_num(n,radix,width,pad="0",force=false)

- is\_bdigit\_ch(ch)
- is\_bdigit\_str(s)
- is\_odigit\_ch(ch)
- is\_odigit\_str(s)
- is\_ddigit\_ch(ch)
- is\_ddigit\_str(s)
- is\_hdigit\_ch(ch)
- is\_hdigit\_str(s)
- is\_ndigit\_ch(n,ch)   //n <=36
- is\_ndigit\_str(n,s)   //n <=36
- slength(s)
- blength(s)
- u16length(s)


- parse\_num(s)               //bigint-str  int-str   0xaf 0b1001 0o555
- is\_jsint\_str(s)           //123   0xaf
- parse\_int(s)               //123   0xaf  0b1001   0o555
- is\_int\_str(s)             //123   0xaf  0b1001   0o555
- is\_ary\_idx\_str(s)        //is\_int\_str(s) && >=0 && < 2**32 -1
- is\_bigint\_str(s)
- is\_float\_str(s)
- parse\_float(s)          //strict  ".5.6" not valid, 
-                          //"." not valid 
-                          //"-." not valid, 
-                          //"-1.1e2.5"   ===  -1.1 * (10 ** 2.5) 
-                          //others same as parseFloat

- LOWER\_CASE\_ATOZ\_STR
- UPPER\_CASE\_ATOZ\_STR
- LOWER\_CASE\_ATOZ\_MD
- UPPER\_CASE\_ATOZ\_MD

- parse0(s,cfg= {only\_value:true,with\_value:true,with\_type:false,unknown\_as\_string:false})
- parse0.unknown


- reverse(s)
- trim\_left(s,cond:String|RegExp|(ch)=\>Boolean)
- trim\_right(s,cond:String|RegExp|(ch)=\>Boolean)
- trim(s,cond:String|RegExp|(ch)=\>Boolean)

- DFLT\_TRIM\_LEFT\_REGEXP
- DFLT\_TRIM\_RIGHT\_REGEXP
- DFLT\_TRIM\_REGEXP

- gen\_split\_two(s)
- gen\_splits(s,n)

- u(s,ignore\_length\_one=false)
- from\_u(s)

- try\_dequote(s,q='"',escc=['\b', '\f', '\n', '\r', '\t', '\x0B' ,' ','"','`',"'"])

- mul(a,b)                  // Params[a:String,b:Int] OR Params[a:Int,b:String]
- add(s0,s1)
- sub(s0,s1)
- length\_div(s0,s1) 
- length\_quo(s0,s1) 
- length\_mod(s0,s1)  
- div(s,n:Int)
- quo(s,n:Int) 
- mod(s,n:Int)
- sdiv(s0,s1)
- squo(s0,s1) 
- smod(s0,s1)

- match_by(s,str\_or\_regexp\_or\_cond\_func\_rtrn\_bool)
- match(s,regex)

- _char_at(s,code_index)

LICENSE
=======
- ISC

---
_Source: https://npm.io/package/nv-string-basic · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
