@alis-build/utils
v0.0.5
Published
A collection of common utilities used at Alis Exchange.
Downloads
65
Readme
ts-alis-build
Alis Build utils for Javascript/Typescript
Installation
npm install @alis-build/utils
Usage
import { strings, numbers, time, money } from "@alis-build/utils";
Time
encodeTimestamp
- Converts aDate
object to agoogle.protobuf.Timestamp
object.
time.encodeTimestamp(new Date())
encodeDate
- Converts aDate
object to agoogle.type.Date
object.
time.encodeDate(new Date())
formatDistance
- Formats the distance between two dates.
time.formatDistance(new Date(), new Date(), true)
Also accepts google.protobuf.Timestamp
and google.type.Date
objects.
parse
- Parses agoogle.protobuf.Timestamp
orgoogle.type.Date
object to aDate
object.
const timestamp = new Timestamp();
timestamp.setSeconds(1634294400);
timestamp.setNanos(0);
time.parse(timestamp)
Money
encode
- Converts a number to agoogle.type.Money
object.
money.encode("USD", 100.20)
format
- Formats agoogle.type.Money
object to a string.
money.format(money.encode("USD", 100.20))
parse
- Parses agoogle.type.Money
object to a number.
const moneyObj = new Money();
moneyObj.setCurrencyCode("USD");
moneyObj.setUnits(100);
moneyObj.setNanos(200000000);
money.parse(moneyObj)
Duration
Duration
- Custom Duration class to handle duration calculations.
const duration = new duration.Duration({ hours: 1, minutes: 30, seconds: 30 });
encode
- Converts aDuration
to agoogle.protobuf.Duration
object.
const duration = new duration.Duration({ hours: 1, minutes: 30, seconds: 30 });
duration.encode()
parse
- Parses agoogle.protobuf.Duration
object to aDuration
.
const durationObj = new Duration();
durationObj.setSeconds(5400);
duration.parse(durationObj)
Strings
snakeCaseToCamelCase
camelCaseToSnakeCase
snakeCaseToKebabCase
kebabCaseToSnakeCase
camelCaseToKebabCase
kebabCaseToCamelCase
snakeCaseToPascalCase
pascalCaseToSnakeCase
kebabCaseToPascalCase
pascalCaseToKebabCase
camelCaseToPascalCase
pascalCaseToCamelCase
toTitleCase
toConstantCase