1.0.12 • Published 1 month ago
nv-facutil-count v1.0.12
nv-facutil-count
- count types
install
- npm install nv-facutil-count
splitted
usage
const {
can_be_json_stringified,
can_be_perfectly_json_stringified,
total,
simple,
detail,
key,
} = require("nv-facutil-count");
example
can_be_json_stringified
JSON.stringify(o) will NOT throw AND
JSON.stringify(o) will return String
similiar to :
(o)=> {
try {
var r = JSON.stringify(o); // some time JSON.stringify will NOT return String: JSON.stringify(undefined) -> Undefined
return(typeof(r) === "string");
} catch(e) {
return(false);
}
}
but NOT use throw
> var a =[]
> a[0]=a
<ref *1> [ [Circular *1] ]
>
> can_be_json_stringified(a)
false
>
> a[0] = true
true
> can_be_json_stringified(a)
true
>
> can_be_json_stringified(()=>{})
false
> JSON.stringify(()=>{}) === undefined
true
>
can_be_perfectly_json_stringified
all leaf node MUST be boolean | number|string|null AND
NOT NaN | Infinity | -Infinity
all container node MUST be array-with-out-attrs OR dict {....} AND
NO circular reference Exist
this is SLOW , it make sure IF JSON.parse(o) AND JSON.stringify(o) IS Symmetric:
> var a = [1,"s"]
> can_be_perfectly_json_stringified(a)
true
>
> a=[new Number(1),"s"]
[ [Number: 1], 's' ]
>
> can_be_perfectly_json_stringified(a)
false
> a=[undefined,"s"]
[ undefined, 's' ]
> can_be_perfectly_json_stringified(a)
false
>
> a=[Infinity,"s"]
[ Infinity, 's' ]
> can_be_perfectly_json_stringified(a)
false
> a=[null,true,false,{},"s",[]]
[ null, true, false, {}, 's', [] ]
> can_be_perfectly_json_stringified(a)
true
> a=[null,true,false,{},"s",[1,2,3]]
[ null, true, false, {}, 's', [ 1, 2, 3 ] ]
> can_be_perfectly_json_stringified(a)
true
>
> a[5].attr = "xxxxx"
'xxxxx'
> a
[ null, true, false, {}, 's', [ 1, 2, 3, attr: 'xxxxx' ] ]
>
> can_be_perfectly_json_stringified(a)
false
>
> delete a[5].attr
true
> can_be_perfectly_json_stringified(a)
true
> a[0] = new Date
2024-04-26T07:23:44.666Z
> a
[ 2024-04-26T07:23:44.666Z, true, false, {}, 's', [ 1, 2, 3 ] ]
> can_be_perfectly_json_stringified(a)
false
const creat_obj_for_test = ()=>{
var st = new Set([])
for(let e of [1,1.1,undefined,null,true,false,"abcd","aÿ我𝑒",12345678901234567890n,[],{}]) {st.add(e)}
var mp = new Map();
let dict = {a:100,b:-0}
let ary = [0,1,2,3,new ArrayBuffer(16)]
for(let e of [[{},[]],[true,false],[dict,dict],[ary,ary]]) {mp.set(e,e)}
for(let e of [[]]) {mp.set(e,e)}
for(let e of [1,1.1,undefined,null,true,false,"abcd","aÿ我𝑒",12345678901234567890n]) {mp.set(e,e)}
var circular = [];
circular[0] = circular
circular[1] = {pr:circular[0]}
circular[2] = mp
st.add(mp);
mp.set("set",st);
var ab = new ArrayBuffer(8);
var u8a = new Uint8Array(ab);
var u16a = new Uint16Array(ab);
mp.set("ab",ab);
mp.set("u8a",u8a);
mp.set("u16a",u16a);
var dv = new DataView(ab,5)
st.add(dv);
ary.date = new Date;
ary.rgx = /[0-9]+/g;
circular.lclsym = Symbol("-");
circular.glbsym = Symbol.for("gsym")
return(circular);
};
total only count value-node
// {k: 999} two: {...} AND 999, simple "k" is NOT counted
// new Map([["k",999]]) three: Map(...) AND "k" AND 999, map-key "k" IS counted
total(creat_obj_for_test())
// 76 BufferView.buffer will be taken into count
// Error.message AND Error.stack AND Error.cause will be taken into count
simple only count value-node
console.log(simple(creat_obj_for_test()))
/*
VStats {
u: 3,
n: 3,
t: 4,
f: 4,
num: 12,
str: 10,
bi: 3,
lsym: 1,
gsym: 1,
dt: 1,
rgx: 1,
ary: 9,
dict: 4,
st: 1,
mp: 1,
ab: 2,
dv: 1,
u8a: 1,
u8ca: 0,
i8a: 0,
u16a: 1,
i16a: 0,
u32a: 0,
i32a: 0,
u64a: 0,
i64a: 0,
f32a: 0,
f64a: 0,
truo: 0,
flso: 0,
numo: 0,
stro: 0,
circular: 13,
wkref_can_be_deref: 0,
err: 0,
others: 0
}
>
*/
detail only count value-node
same as simple, with more detailed info
VDetailedStats {
u: 0,
n: 0,
t: 0,
f: 0,
num: 0,
str: 0,
bi: 0, //BigInt
lsym: 0, //Symbol created by Symbol("...")
gsym: 0, //Symbol created by Symbol.for("...")
dt: 0, // Date
rgx: 0, // RegExp
ary: 0,
dict: 0,
st: 0, //Set
mp: 0, //Map
ab: 0, //ArrayBuffer OR SharedArrayBuffer (for compatible with browser, NOT diff from AB with SAB)
dv: 0, //DataView
u8a: 0,
u8ca: 0,
i8a: 0,
u16a: 0,
i16a: 0,
u32a: 0,
i32a: 0,
u64a: 0,
i64a: 0,
f32a: 0,
f64a: 0,
truo: 0, // new Boolean(true)
flso: 0, // new Boolean(false)
numo: 0, // new Number
stro: 0, // new String
circular: 0, // Reference such as: WHEN <o={}; o.self = o> THEN <o IS dict AND o.self IS circular>
wkref_can_be_deref: 0, // WeakRef && .deref() !== undefined
wkst: 0, // WeakSet
wkmp: 0, // WeakMap
wkref_can_not_be_deref: 0, // WeakRef && .deref() === undefined
prms: 0, // Promise
cls: 0, // class
iter: 0, // Iterator such as Map'entries()
sg: 0, // sync generator
ag: 0, // async generator
sfunc: 0, // function (...){......}
afunc: 0, // async function (...){......}
sgen: 0, // function* (...){......}
agen: 0, // async* function (...){......}
slmbd: 0, // (...)=>{......}
almbd: 0, // async ...)=>{......}
smthd: 0, // <class { mthd(...){......}}> ' mthd
amthd: 0, // <class { async mthd(...){......}}> ' mthd
mthd_sgen: 0, // <class { * mthd(...){......}}> ' mthd
mthd_agen: 0, // <class { async* mthd(...){......}}> ' mthd
getter: 0, // <class { get getter(...){......}}> ' getter
setter: 0, // <class { set setter(...){......}}> ' setter
eval_err: 0,
rng_err: 0,
ref_err: 0,
typ_err: 0,
uri_err: 0,
agg_errs: 0, // AggregatorErrors
other_err: 0,
others: 0
}
>
keys
class KStats {
top=1 ; // wkref.deref(if exist); abvw.buffer; err.message err.stack err.cause agg.errors
dict_key= 0; // always be string, include attr OF a=[] a.attr = "xxx"
dict_val= 0; // include "xxx" OF a=[] a.attr = "xxx"
ary_ele = 0;
st_ele = 0;
mpk= 0; mpv= 0;
////
}
key(creat_obj_for_test())
KStats {
top: 4,
dict_key: 4,
dict_val: 4,
ary_ele: 16,
st_ele: 13,
mpk: 18,
mpv: 18
}
METHODS
APIS
LICENSE
- ISC