Skip to content

formatter.js

James ZHANG edited this page Jan 19, 2022 · 5 revisions

Formatter

Provides easy used methods using in this project.

Kind: global class

Formatter.castCsvBill(buffer) ⇒ object

Cast the CSV bill.

Kind: static method of Formatter
Returns: object - - The casted source intputs as {rows, summary} Object

Param Type Description
buffer string | Buffer CSV file content.

Formatter.castCsvLine(row, [keys], [skipFirstChar], [separator]) ⇒ object

Cast the CSV line string by the keys named object.

Kind: static method of Formatter
Returns: object - - The casted source line as Object

Param Type Default Description
row string CSV line.
[keys] Array.<string> CSV headers.
[skipFirstChar] boolean true Skip the first character of the CSV line, default is true.
[separator] string "',`'" Split separator, default is ',`' (two chars).

Formatter.nonce([size]) ⇒ string

Generate a Base62 random string aka nonce, similar as crypto.randomBytes.

Kind: static method of Formatter
Returns: string - - Base62 random string.

Param Type Default Description
[size] number 32 Nonce string length, default is 32 bytes.

Formatter.timestamp() ⇒ number

Retrieve the current Unix timestamp.

Kind: static method of Formatter
Returns: number - Epoch timestamp.

Formatter.authorization(mchid, nonce, signature, timestamp, serial) ⇒ string

Formatting for the heading Authorization value.

Kind: static method of Formatter
Returns: string - - The APIv3 Authorization header value

Param Type Description
mchid string | number The merchant ID.
nonce string The Nonce string.
signature string The base64-encoded Rsa.sign ciphertext.
timestamp string | number The Unix timestamp.
serial string The serial number of the merchant public certification.

Formatter.request(method, uri, timestamp, nonce, body) ⇒ string

Formatting this HTTP.request for Rsa.sign input.

Kind: static method of Formatter
Returns: string - - The content for Rsa.sign

Param Type Description
method string The HTTP verb, must be the uppercase sting.
uri string Combined string with URL.pathname and URL.search.
timestamp string | number The Unix timestamp, should be the one used in authorization.
nonce string The Nonce string, should be the one used in authorization.
body string The playload string, HTTP GET should be an empty string.

Formatter.response(timestamp, nonce, body) ⇒ string

Formatting this HTTP.response for Rsa.verify input.

Kind: static method of Formatter
Returns: string - - The content for Rsa.verify

Param Type Description
timestamp string | number The Unix timestamp, should be the one from response.headers[wechatpay-timestamp].
nonce string The Nonce string, should be the one from response.headers[wechatpay-nonce].
body string The response payload string, HTTP status(204) should be an empty string.

Formatter.joinedByLineFeed(...pieces) ⇒ string

Joined this inputs by for Line Feed(LF) char.

Kind: static method of Formatter
Returns: string - - The joined string.

Param Type Description
...pieces Array.<string> The string(s) joined by line feed.

Formatter.ksort(thing) ⇒ object

Sorts an Object by key.

Kind: static method of Formatter
Returns: object - - The sorted object.

Param Type Description
thing object The input object.

Formatter.queryStringLike(thing) ⇒ string

Like queryString does but without the sign and empty value entities.

Kind: static method of Formatter
Returns: string - - The sorted object.

Param Type Description
thing object The input object.