A TypeError is thrown if records is not an array with at least one element
getKeys
Function
Y
Function to generate keys for each record
Should take a record as the sole param, and return a single key or array of keys; signature is getKeys(record: object): Array<T>
Return value
Type
Description
Array\>
Each element of the returned array is a group of records for which at least one key matched
情况可能并非如此一组中的所有记录都有一个公共密钥。 此外,组中可能有两个记录没有共同的密钥。 例如,下面的三个记录将在同一组中,通过 A 和 C 没有公共键,因为它们与另一条记录有一个公共键在组中:
Record A with keys ['a', 'b']
Record B with keys ['b', 'c']
Record C with keys ['c', 'd']
Usage
查看lib/group-by-multi-keys.test.js中的测试使用示例
formatMobileNumber(mobileNumber, formatString)
以不同方式格式化手机号码(必须包括国家代码)
Parameters
Name
Type
Required?
Description
Default
Comments
mobileNumber
string
Y
The mobile number to be formatted
An error is thrown if mobileNumber does not contain country code
formatString
string
N
String describing how the provided mobile number should be formatted
"+CASR"
在formatString< ,只有以下字符将被转换:
Character
Transformed into
C
country code
A
area code (first 3 digits after country code)
S
subscriber number (next 3 digits)
R
remaining digits
任何其他字符将原样出现在返回
Return value
Type
Description
string
The formatted mobile number
Usage
const { formatMobileNumber: format } = require('@ack_inc/utils');
console.log(format(<anything but a mobile number with country code>)); //=> throws Error
const mobile = "+1 123 456 7890";
expect(format(mobile)).toEqual("+11234567890");
expect(format(mobile, "+CASR")).toEqual("+11234567890");
expect(format(mobile, "+C A S R")).toEqual("+1 123 456 7890");
expect(format(mobile, "+C (A) S-R")).toEqual("+1 (123) 456-7890");
buildArr(n, generatorFn)
中
Parameters
Name
Type
Required?
Description
Default
Comments
n
number
Y
The number of elements in the created array
generatorFn
function
N
Function that controls the values of the elements in the created array
Group an array of records into an array of sub-arrays of records. Records go into the same sub-array ("group") if any of the keys generated by getKeys are === equal
Use case: you have an array of records (of people, say); the records are from multiple sources, and you want to merge records (for belonging to the same person) if any of several different "identifiers" (email, phone number, social media handle, etc.) match
Parameters
Name
Type
Required?
Description
Default
Comments
records
Array\
Y
Array of records to group
A TypeError is thrown if records is not an array with at least one element
getKeys
Function
Y
Function to generate keys for each record
Should take a record as the sole param, and return a single key or array of keys; signature is getKeys(record: object): Array<T>
Return value
Type
Description
Array\>
Each element of the returned array is a group of records for which at least one key matched
It may not be the case that there is a common key for all the records in a group. Further, there may be two records in a group that have no keys in common. For example, the following three records will be in the same group, through A and C do not have a common key, because they each have a key in common with another record in the group:
Record A with keys ['a', 'b']
Record B with keys ['b', 'c']
Record C with keys ['c', 'd']
Usage
See the tests in lib/group-by-multi-keys.test.js for usage examples
formatMobileNumber(mobileNumber, formatString)
Format a mobile number (must include country code) in different ways
Parameters
Name
Type
Required?
Description
Default
Comments
mobileNumber
string
Y
The mobile number to be formatted
An error is thrown if mobileNumber does not contain country code
formatString
string
N
String describing how the provided mobile number should be formatted
"+CASR"
In formatString, only the following characters will be transformed:
Character
Transformed into
C
country code
A
area code (first 3 digits after country code)
S
subscriber number (next 3 digits)
R
remaining digits
Any other characters will appear as-is in the returned string
Return value
Type
Description
string
The formatted mobile number
Usage
const { formatMobileNumber: format } = require('@ack_inc/utils');
console.log(format(<anything but a mobile number with country code>)); //=> throws Error
const mobile = "+1 123 456 7890";
expect(format(mobile)).toEqual("+11234567890");
expect(format(mobile, "+CASR")).toEqual("+11234567890");
expect(format(mobile, "+C A S R")).toEqual("+1 123 456 7890");
expect(format(mobile, "+C (A) S-R")).toEqual("+1 (123) 456-7890");
buildArr(n, generatorFn)
Format a mobile number (must include country code) in different ways
Parameters
Name
Type
Required?
Description
Default
Comments
n
number
Y
The number of elements in the created array
generatorFn
function
N
Function that controls the values of the elements in the created array