unit-compare
v1.0.1
Published
Compare byte and datetime units
Downloads
272,827
Maintainers
Readme
unit-compare
Compare date/time and byte units
Installation
npm install --save unit-compare
Features
- Compares bytes sizes
- Compares ISO datetimes
Usage
The example below asserts if a number if less then 10 megabytes:
const compare = require('unit-compare');
const n = 1024;
const isLessThen10Mb = compare.isNumber(n).assert('<10mb')
if (isLessThen10Mb) {
console.log(`${n} is less than 10mb`);
}
Byte unit comparisons
Assert if a given number against a string byte size expression:
const greaterThan10kb = compare.isNumber(n).assert('>10kb')
Using comparison methods directly:
const greaterThan10kb = compare.isNumber(n).lessThan(10, 'k');
Compare dates with a string expression
Assert if a given number against a string datetime expression:
const date = moment();
const equalTo10Mins = compare.isDate(date).assert('>10 minutes');
Using comparison methods directly:
const date = moment().subtract(10, 'minutes');
const greaterThan10kb = compare.isDate(date).lessThan(10, 'days');
API
compare.isDate() -> DateAssertion
Parameters
- ISO string date
Returns
Returns an DateAssertion instance.
compare.isNumber() -> ByteAssertion
Parameters
- Accepts an integer
Returns
Returns a ByteAssertion instance.
ByteAssertion
.assert(sizeExpression) -> boolean
Parameters
sizeExpression - accepts a positive integer representing the file size. File size units are:
- bytes, specified using b.
- kilobytes, specified using k or kb,
- megabytes, specified using m or mb
- terabytes, specified using t or tb
- gigabytes, specified using g or gb
If no unit is specified, bytes is used by default.
Optionally, expressions can be prefixed with a comparison operator, including:
- less than using <
- greater than using >
- equality using == or =
- less than or equal to using <=
- greater than or equal to >=
Examples:
- equal to 10 bytes: 10
- equal to 10 bytes: ==10b
- less than 10 bytes: <10
- greater than 50 megabytes: >10mg
.lessThan(int, unit) -> boolean
Parameters
- integer
- unit - k, m, g, t
.greaterThan(int, unit) -> boolean
Parameters
- integer
- unit - k, m, g, t
.equalTo(int, unit) -> boolean
Parameters
- integer
- unit - k, m, g, t
.greaterThanOrEqual(int, unit) -> boolean
Parameters
- integer
- unit - k, m, g, t
.lessThanOrEqual(int, unit) -> boolean
Parameters
- integer
- unit - k, m, g, t
DateAssertion
Parameters
dateExpression - accepts a time unit. Time units are:
- minutes, specified using minutes, m, mins, min.
- hours, specified using hours, h, hour.
- days, specified using days, d, day.
If no unit is specified, days is used by default.
Optionally, expressions can be prefixed with a comparison operator, including:
- less than using <
- greater than using >
- equality using == or =
If no comparison operator is specified, equality is used by default.
Examples:
- equal to 10 days: 10
- equal to 10 minutes: == minutes
- less than 10 hours: < 10 hours
- greater than 50 minutes: >50minutes
- less than 50 minutes: <50m
Returns
- Returns boolean
.lessThan(int, timeUnit) -> boolean
Parameters
- integer
- timeUnit - minutes, hours, days
.greaterThan(int, timeUnit) -> boolean
Parameters
- integer
- timeUnit - minutes, hours, days
.equalTo(int, timeUnit) -> boolean
Parameters
- integer
- timeUnit - minutes, hours, days
.greaterThanOrEqual(int, timeUnit) -> boolean
Parameters
- integer
- timeUnit - minutes, hours, days
.lessThanOrEqual(int, timeUnit) -> boolean
Parameters
- integer
- timeUnit - minutes, hours, days
Test
npm test
To generate a test coverage report:
npm run coverage
Contributing
- If you're unsure if a feature would make a good addition, you can always create an issue first.
- We aim for 100% test coverage. Please write tests for any new functionality or changes.
- Any API changes should be fully documented.
- Make sure your code meets our linting standards. Run
npm run lint
to check your code. - Maintain the existing coding style. There are some settings in
.jsbeautifyrc
to help. - Be mindful of others when making suggestions and/or code reviewing.