Skip to content

Tairraos/xTool.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

64 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

要跑UT请先安装Jest: npm i jest

xtool

Enchance file, number feature of node

xFile

File relative tools of xTool

xHtml

HTML relative tools of xTool

xNumber

Number relative tools of xTool

xUtil

Utility of xTool

Functions

prepareDir(dir)

Prepare directory, will create the full path if not exist

readDir(root, [setting]) => array

Get file list from a root path with configure

readFile(file, [encoding]) => string

Read file content

existFile(file) => boolean

Test file exist or not

existDir(dir) => boolean

Test dir exist or not

saveFile(file, content, [encoding])

Save content to file

removeFile(file) => boolean

Alias of fs.unlinkSync

scanFile(file, callback)

scan file by line, and do callback to content of each line

scanListFile(list, callback)

scan all file in list by line, and do callback to content of each line

replaceFile(file, pattern|callback, [replacement])

replace file content with pattern & replacement

decodeHtml(htmlContent) => string

transfer &#DDDD; & &#xHHHH; it was. make sure the html page under utf-8;

tolerant(num) => string

修正中文数字常见的错误,并转小写

numberChnToBig(num) => string

中文数字小写转大写,“万”,“亿”是不区分大小写的

numberChnToSmall(num) => string

中文数字小写转大写,“万”,“亿”是不区分大小写的

numberAri2Chn(num) => string

阿拉伯数字转中文数字

numberChn2Ari(num) => number

中文数字转阿拉伯数字

isLegalChnNum(num) => string

检查中文数字合法性,能通过基本容错也为合法

numberAri2Roman(num) => string

阿拉伯数字转罗马数字

numberRoman2Ari(s) => number

罗马数字转阿拉伯数字

typeof(arg) => string

return the truly type of arg

is(value, type) => boolean

compare the value type with expect type

flattenArray(arg) => array

recursive embeded array to a flat array, and will remove duplicated value

distinctArray(arg) => array

remove duplicated value of array

getArgs() => object

get node command args

range(start, [end]) => array

generate values array of specified range, number range should be 0 to 2^32

xFile

File relative tools of xTool

Kind: global variable

xHtml

HTML relative tools of xTool

Kind: global variable

xNumber

Number relative tools of xTool

Kind: global variable

xUtil

Utility of xTool

Kind: global variable

prepareDir(dir)

Enchance file, number feature of node

Kind: global function

Param Type Description
dir string the path want to prepared

Kind: global variable

readDir(root, [setting]) => array

Get file list from a root path with configure

Kind: global function
Returns: array - list of file with absolute/relative path

Param Type Description
root string file root path
[setting] object | string optional, if only one find pattern, can be simplify as a string
[setting.absolute] boolean return absolute path or path relative from root. [default is false]
[setting.recursive] boolean recursive to sub directory. [default is false]
[setting.find] string | regexp | array provide human pattern or human pattern list to define filename matcher. [default is "*"]
[setting.ignore] string | regexp | array provide human pattern or human pattern list to define which filename will be ignored. [default is none]

readFile(file, [encoding]) => string

Read file content

Kind: global function
Returns: string - if file not exist, will return empty string

Param Type Description
file string path to file
[encoding] string encoding

existFile(file) => boolean

Test file exist or not

Kind: global function

Param Type Description
file string path to file

existDir(dir) => boolean

Test dir exist or not

Kind: global function

Param Type Description
dir string path

saveFile(file, content, [encoding])

Save content to file

Kind: global function

Param Type Description
file string path to file
content string | array | object file content, array will be join with {crlf}, object will be str
[encoding] string encoding

removeFile(file) => boolean

Alias of fs.unlinkSync

Kind: global function
Returns: boolean - if file not exist, return false

Param Type Description
file string path to file

scanFile(file, callback)

scan file by line, and do callback to content of each line

Kind: global function

Param Type Description
file string filename
callback function call back func(lineContent, lineNumber)

scanListFile(list, callback)

scan all file in list by line, and do callback to content of each line

Kind: global function

Param Type Description
list array the file list from readDir()
callback function call back func(lineContent, lineNumber, fileName)

replaceFile(file, pattern|callback, [replacement])

replace file content with pattern & replacement

Kind: global function

Param Type Description
file string path to file
pattern callback string | regexp | function
[replacement] string if pattern provided, replacement must provide, if pattern is regexp, replacement can use the captured value by $1, $2, etc.

decodeHtml(htmlContent) => string

transfer &#DDDD; & &#xHHHH; it was. make sure the html page under utf-8;

Kind: global function

Param Type Description
htmlContent string included

tolerant(num) => string

修正中文数字常见的错误,并转小写

Kind: global function
Returns: string - 不校验数字拼写是否正确,只转换常错的拼写

Param Type Description
num string 中文数字

numberChnToBig(num) => string

中文数字小写转大写,“万”,“亿”是不区分大小写的

Kind: global function
Returns: string - 不校验数字拼写是否正确,只转换大写

Param Type Description
num string 中文数字

numberChnToSmall(num) => string

中文数字小写转大写,“万”,“亿”是不区分大小写的

Kind: global function
Returns: string - 不校验数字拼写是否正确,只转换大写

Param Type Description
num string 中文数字

numberAri2Chn(num) => string

阿拉伯数字转中文数字

Kind: global function
Returns: string - 返回中文数字

Param Type Description
num number 阿拉伯数字,大于零,小于一亿亿。不要使用巨大数字,受V8引擎精度限制,10^16左右就会精度失真。Math.pow(10,16) - 1 === Math.pow(10,16) 为 true

numberChn2Ari(num) => number

中文数字转阿拉伯数字

Kind: global function
Returns: number - 返回阿拉伯数字, 返回-1表示中文数字非法

Param Type Description
num string 中文数字,大于零,小于一亿亿。不要使用巨大数字。接近或大于一亿亿的时候,V8引擎下的整数会出现精度问题。

isLegalChnNum(num) => string

检查中文数字合法性,能通过基本容错也为合法

Kind: global function
Returns: string - true表示合法中文数字

Param Type Description
num string 中文数字,大于零,小于一亿亿

numberAri2Roman(num) => string

阿拉伯数字转罗马数字

Kind: global function

Param Type Description
num number 范围0-3999

numberRoman2Ari(s) => number

罗马数字转阿拉伯数字

Kind: global function

Param Type Description
s string 范围0-3999

typeof(arg) => string

return the truly type of arg

Kind: global function
Returns: string - type of arg

Param Type Description
arg * any type arg

is(value, type) => boolean

compare the value type with expect type

Kind: global function

Param Type Description
value * value will be test
type string expect type

flattenArray(arg) => array

recursive embeded array to a flat array, and will remove duplicated value

Kind: global function
Returns: array - return a one dimension array

Param Type Description
arg array | * multi dimensions array, other type of arg will return a array warped arg

distinctArray(arg) => array

remove duplicated value of array

Kind: global function
Returns: array - return an array with every unquie value

Param Type Description
arg array | * array to remopve duplicated value

getArgs() => object

get node command args

Kind: global function
Returns: object - - command line: "node xxx.js -a 111 -b=222 333 -d", will return { 0: ["-a", "111", "-b=222", "333", "-d"], 1: "-a", 2: "111", 3: "-b=222", 4: "333", "5": "-d", "-a": "111", "-b": "222", "-d": true }

range(start, [end]) => array

generate values array of specified range, number range should be 0 to 2^32

Kind: global function

Param Type Description
start number | string if "end" is not provided, the range is (0 or "0" or "a" or "A") to "start"
[end] number | string

About

Some JS tools made by me.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published