This commit is contained in:
root
2023-03-29 15:20:05 +00:00
parent 5ec489e0e0
commit a0bb8f2d1e
25468 changed files with 3063105 additions and 28 deletions
+77
View File
@@ -0,0 +1,77 @@
var MILLISECONDS_IN_DAY = 86400000;
var MILLISECONDS_IN_HOUR = 3600000;
var MILLISECONDS_IN_MINUTE = 60000;
var HOURS_IN_DAY = 24;
var MINUTES_IN_DAY = 1440;
var MINUTES_IN_HOUR = 60;
/**
* @param {Number} milliseconds
* @return {Number}
* @private
*/
function _getDaysDiff(milliseconds) {
return Math.abs(Math.floor(milliseconds / MILLISECONDS_IN_DAY));
}
/**
* @param {Number} milliseconds
* @return {Number}
* @private
*/
function _getHoursDiff(milliseconds) {
return Math.abs(Math.floor((milliseconds % MILLISECONDS_IN_DAY) / MILLISECONDS_IN_HOUR));
}
/**
* @param {Number} milliseconds
* @return {Number}
* @private
*/
function _getMinutesDiff(milliseconds) {
return Math.abs(Math.round(((milliseconds % MILLISECONDS_IN_DAY) % MILLISECONDS_IN_HOUR) / MILLISECONDS_IN_MINUTE));
}
/**
* @param {Number} milliseconds
* @return {Number}
* @private
*/
function _differenceInHours(milliseconds) {
var days = _getDaysDiff(milliseconds);
if (days !== 0) {
return _getHoursDiff(milliseconds) + (days * HOURS_IN_DAY);
}
return _getHoursDiff(milliseconds);
}
/**
* @param {Number} milliseconds
* @return {Number}
* @private
*/
function _differenceInMinutes(milliseconds) {
var days = _getDaysDiff(milliseconds),
hours = _getHoursDiff(milliseconds);
if (days !== 0) {
days = days * MINUTES_IN_DAY;
}
if (hours !== 0) {
hours = hours * MINUTES_IN_HOUR;
}
return _getMinutesDiff(milliseconds) + days + hours;
}
module.exports = {
_getDaysDiff: _getDaysDiff,
_getHoursDiff: _getHoursDiff,
_getMinutesDiff: _getMinutesDiff,
_differenceInHours: _differenceInHours,
_differenceInMinutes: _differenceInMinutes
};
+17
View File
@@ -0,0 +1,17 @@
language: node_js
node_js:
- stable
branches:
only:
- master
install:
- npm install
script:
- npm run test
after_success:
- npm run report-coverage
+9
View File
@@ -0,0 +1,9 @@
## 2.1.0 (July 9, 2018)
* Added [objectInterface](docs/objectInterface.md) (in [#2](https://github.com/shystruk/famulus/pull/2))
## 2.0.0 (June 26, 2018)
* Added [dateDifferenceFromNow](docs/dateDifferenceFromNow.md) (in [#1](https://github.com/shystruk/famulus/pull/1))
* [dateDifference](docs/dateDifference.md) compares two dates (in [#1](https://github.com/shystruk/famulus/pull/1))
+39
View File
@@ -0,0 +1,39 @@
# Contributing
✨ Thank you for contributing ✨
Please feel free to contribute by submitting PR's with your own helper, improvement to code snippets, explanations, etc.
## Submitting an issue
Found a problem? Have an enhancement?
First of all see if your issue or idea has already been [reported](https://github.com/shystruk/famulus/issues).
If do not, open a [new one](https://github.com/shystruk/famulus/issues/new).
## Add your own helper
- Please take for example helper **isValuesUnique.js**
- Create *[helper_name].js* file in the root folder
- In docs folder create *[helper_name].md* file and fill in data keeping the structure
- In tests folder create *[helper_name].test.js* file and cover the helper by unit tests and much as possible
- In famulus.js file import and export already created helper
- In documentation section at README.md file add the helper to a specific category. If the category does not exist add the new one
- Before submitting a PR please check *Submitting a pull request* section below
## Submitting a pull request
- Fork this repository
- Clone fork `git clone ...`
- Navigate to the cloned directory
- Install all dependencies `npm install`
- Crate a new branch for the feature `git checkout -b new-feature`
- Make changes
- Run tests `npm run test`
- Commit changes `git commit -am 'What is feature about? :)'`
- Push to the branch `git push origin new-feature`
- Submit a PR
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) Vasyl Stokolosa <v.stokol@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+56
View File
@@ -0,0 +1,56 @@
# famulus [![Twitter URL](https://img.shields.io/twitter/url/http/shields.io.svg?style=social)](https://twitter.com/intent/tweet?hashtags=javascript%20%23helpers%20%23utilities&original_referer=https%3A%2F%2Fpublish.twitter.com%2F&ref_src=twsrc%5Etfw&text=JavaScript%20library%20that%20provides%20a%20useful%20functional%20programming%20helpers.%20Do%20not%20wait%2C%20add%20your%20own%20%F0%9F%94%A7%F0%9F%92%AA&tw_p=tweetbutton&url=https%3A%2F%2Fwww.npmjs.com%2Fpackage%2Ffamulus&via=shystrukk) #
[![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php) [![codecov](https://codecov.io/gh/shystruk/famulus/branch/master/graph/badge.svg)](https://codecov.io/gh/shystruk/famulus) [![Build Status](https://travis-ci.org/shystruk/famulus.svg?branch=master)](https://travis-ci.org/shystruk/famulus)
[![Known Vulnerabilities](https://snyk.io/test/github/shystruk/famulus/badge.svg?targetFile=package.json)](https://snyk.io/test/github/shystruk/famulus?targetFile=package.json)
[![npm version](https://badge.fury.io/js/famulus.svg)](https://badge.fury.io/js/famulus)
JavaScript library that provides a useful functional programming helpers. Do not wait, fork and add your own 🔧💪
## Installation
#### npm
`npm install --save famulus`
#### yarn
`yarn add famulus --save`
## Usage
#### ES6 module
```javascript
import famulus from 'famulus'
```
```javascript
import helper from 'famulus/helper'
```
#### Node.js
```javascript
const famulus = require('famulus')
```
```javascript
const helper = require('famulus/helper')
```
## Documentation
### String
- [substr](docs/substr.md)
### Object
- [objectInterface](docs/objectInterface.md)
### Array
- [sortAndAddFirstElement](docs/sortAndAddFirstElement.md)
- [isValuesUnique](docs/isValuesUnique.md)
### Date
- [dateDifferenceFromNow](docs/dateDifferenceFromNow.md)
- [dateDifference](docs/dateDifference.md)
## Contributing
Please read the [Contributions Guidelines](CONTRIBUTING.md) before adding your own helper or improvement to code snippets, explanations, etc.
## License
MIT © [Vasyl Stokolosa](https://about.me/shystruk)
+57
View File
@@ -0,0 +1,57 @@
var dates = require('./.internal/dates');
/**
* Difference between dates which are passed, in formats 'milliseconds', 'days', 'hours', 'minutes'
*
* @customNeeds -
*
* @since 2.0.0
* @category Date
*
* @param {Date} date1 - The Date for compare
* @param {Date} date2 - The Date for compare
* @param {String} differenceType - [ 'days', 'hours', 'minutes', 'milliseconds', 'all' ]
*
* @returns {Number|Object} Returns the numeric value or object depends on passed differenceType param
*
* @example
*
* famulus.dateDifference(new Date('06-20-2018'), new Date('06-26-2018'), 'days')
* // => 6
*
* famulus.dateDifference(new Date('06-20-2018'), new Date('06-26-2018'), 'hours')
* // => 144
*
* famulus.dateDifference(new Date('06-20-2018'), new Date('06-26-2018'), 'minutes')
* // => 8640
*
* famulus.dateDifference(new Date('06-26-2018'), new Date('06-20-2018'), 'milliseconds')
* // => 518400000
*
* famulus.dateDifference(new Date('06-26-2018 10:10'), new Date('06-20-2018 08:00'), 'all')
* // => {days: 6, hours: 2, minutes: 10, milliseconds: 526200000}
*/
function dateDifference(date1, date2, differenceType) {
var diffMilliseconds = Math.abs(date1 - date2);
switch(differenceType) {
case 'days':
return dates._getDaysDiff(diffMilliseconds);
case 'hours':
return dates._differenceInHours(diffMilliseconds);
case 'minutes':
return dates._differenceInMinutes(diffMilliseconds);
case 'milliseconds':
return diffMilliseconds;
default:
return {
days: dates._getDaysDiff(diffMilliseconds),
hours: dates._getHoursDiff(diffMilliseconds),
minutes: dates._getMinutesDiff(diffMilliseconds),
milliseconds: diffMilliseconds
}
}
}
module.exports = dateDifference;
@@ -0,0 +1,52 @@
var dates = require('./.internal/dates');
/**
* Difference between now and date which is passed, in formats 'milliseconds', 'days', 'hours', 'minutes'
*
* @customNeeds -
*
* @since 2.0.0
* @category Date
*
* @param {Date} date - The Date to inspect
* @param {String} differenceType - [ 'days', 'hours', 'minutes', 'milliseconds', 'all' ]
*
* @returns {Number|Object} Returns the numeric value or object depends on passed differenceType param
*
* @example
* example result for now is new Date('12-26-2017')
*
* famulus.dateDifferenceFromNow(new Date('12-20-2017'), 'milliseconds')
* // => 555261242
*
* famulus.dateDifferenceFromNow(new Date('12-20-2017'), 'days')
* // => 6
*
* famulus.dateDifferenceFromNow(new Date('12-20-2017'), 'hours')
* // => 156
*/
function dateDifferenceFromNow(date, differenceType) {
var now = new Date(),
diffMilliseconds = Math.abs(date - now);
switch(differenceType) {
case 'days':
return dates._getDaysDiff(diffMilliseconds);
case 'hours':
return dates._differenceInHours(diffMilliseconds);
case 'minutes':
return dates._differenceInMinutes(diffMilliseconds);
case 'milliseconds':
return diffMilliseconds;
default:
return {
days: dates._getDaysDiff(diffMilliseconds),
hours: dates._getHoursDiff(diffMilliseconds),
minutes: dates._getMinutesDiff(diffMilliseconds),
milliseconds: diffMilliseconds
}
}
}
module.exports = dateDifferenceFromNow;
@@ -0,0 +1,47 @@
## dateDifference(date1, date2, differenceType)
[Source](../dateDifference.js)
Difference between dates which are passed, in formats 'milliseconds', 'days', 'hours', 'minutes'
#### Custom Needs
#### Since
2.0.0
#### Category
Date
#### Arguments
{Date} date1 - The Date for compare<br>
{Date} date2 - The Date for compare<br>
{String} differenceType - [ 'days', 'hours', 'minutes', 'milliseconds', 'all' ]
#### Returns
{Number|Object} Returns the numeric value or object depends on passed differenceType param
#### Example
```javascript
dateDifference(new Date('06-20-2018'), new Date('06-26-2018'), 'days')
// => 6
```
```javascript
dateDifference(new Date('06-20-2018'), new Date('06-26-2018'), 'hours')
// => 144
```
```javascript
dateDifference(new Date('06-20-2018'), new Date('06-26-2018'), 'minutes')
// => 8640
```
```javascript
dateDifference(new Date('06-26-2018'), new Date('06-20-2018'), 'milliseconds')
// => 518400000
```
```javascript
dateDifference(new Date('06-26-2018 10:10'), new Date('06-20-2018 08:00'), 'all')
// => {days: 6, hours: 2, minutes: 10, milliseconds: 526200000}
```
@@ -0,0 +1,47 @@
## dateDifferenceFromNow(date, differenceType)
[Source](../dateDifferenceFromNow.js)
Difference between now and date which is passed, in formats 'milliseconds', 'days', 'hours', 'minutes'
#### Custom Needs
#### Since
2.0.0
#### Category
Date
#### Arguments
{Date} date - The Date to inspect<br>
{String} differenceType - [ 'days', 'hours', 'minutes', 'milliseconds', 'all' ]
#### Returns
{Number|Object} Returns the numeric value or object depends on passed differenceType param
#### Example
Example result for now is Date('12-26-2017')
```javascript
dateDifferenceFromNow(new Date('12-20-2017'), 'days')
// => 6
```
```javascript
dateDifferenceFromNow(new Date('12-20-2017'), 'hours')
// => 156
```
```javascript
dateDifferenceFromNow(new Date('12-20-2017'), 'minutes')
// => 9381
```
```javascript
dateDifferenceFromNow(new Date('12-20-2017'), 'milliseconds')
// => 555261242
```
```javascript
dateDifferenceFromNow(new Date('12-20-2017'), 'all')
// => {days: 6, hours: 12, minutes: 30, milliseconds: 563406381}
```
@@ -0,0 +1,33 @@
## isValuesUnique(array, keyName)
[Source](../isValuesUnique.js)
Checking if values are unique
#### Custom Needs
Having an array of objects and checking if values are unqiue by object key
#### Since
1.3.0
#### Category
Array
#### Arguments
{Array} array - The array of objects<br>
{String} keyName - Name of the object property from an array in which unique will be checking<br>
#### Returns
{Boolean} Returns true if values are unique and false if not
#### Example
Unique emails
```javascript
isValuesUnique([{email: 'api@test.com'}, {email: 'api@test.com'}], 'email');
// => false
```
Emails are not unique
```javascript
isValuesUnique([{email: 'api@test.com'}, {email: 'api_1@test.com'}], 'email');
// => true
```
@@ -0,0 +1,35 @@
## objectInterface(config)(object)
[Source](../objectInterface.js)
Interface for building an object by configuration
#### Custom Needs
Have an interface for building an object based on configuration
#### Since
2.1.0
#### Category
Object
#### Arguments
{Array} config - Keys with configuration<br>
```
[
'key/value' - "OR" if no value, set value after "/",
'key|this.firstName + " " + this.lastName' - set value from the expression after "|" which is bind to the passed object,
'key:new Date()' - set value from the expression after ":"
]
```
#### Returns
{Object}
#### Example
```javascript
var email = objectInterface(['body', 'count/1', 'sender|this.firstName + " " + this.lastName', 'isRead: false', 'created: new Date()'])
// => function
email({body: 'Hello world!', count: '', firstName: 'Vasyl', lastName: 'Stokolosa', another: ''})
// => {body: "Hello world!", count: 1, created: Mon Jul 09 2018 10:31:08, isRead: false, sender: "Vasyl Stokolosa"}
```
@@ -0,0 +1,28 @@
## sortAndAddFirstElement(array, sortBy, element)
[Source](../sortAndAddFirstElement.js)
Sort an array by the name of existing property and add a first element into array
#### Custom Needs
Sort array by name and add first element<br>
For e.g. user names - [ {name: 'All'}, {name: 'Aron'}, {name: 'Bob'} ]
#### Since
1.1.0
#### Category
Array
#### Arguments
{Array} array - The array to sort and add<br>
{String} sortBy - Name of the property from an array by which array will be sorted<br>
{*Any} element - Element which is added into an array
#### Returns
{Array} Returns the new array
#### Example
```javascript
sortAndAddFirstElement([{name:'Bob'}, {name:'Aron'}], 'name', {name:'All'});
// => [ {name:'All'}, {name:'Aron'}, {name:'Bob'} ]
```
+38
View File
@@ -0,0 +1,38 @@
## substr(string, start, length)
[Source](../substr.js)
Extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters.
The substr() does not change the original string.
#### Custom Needs
Validate string type for preventing SyntaxError
#### Since
1.0.0
#### Category
String
#### Arguments
{String} string - The string to extract<br>
{Number} start - The position where to start the extraction. First character is at index 0<br>
{Number?} length - Optional. The number of characters to extract. If omitted, it extracts the rest of the string
#### Returns
{String} Returns extract part of a string
#### Example
```javascript
substr('Hello World!', 0, 5)
// => 'Hello'
```
```javascript
substr({}, 0, 5)
// => {}
```
```javascript
substr('Hello World!', 6)
// => 'World!'
```
+13
View File
@@ -0,0 +1,13 @@
var substr = require('./substr');
var sortAndAddFirstElement = require('./sortAndAddFirstElement');
var dateDifferenceFromNow = require('./dateDifferenceFromNow');
var isValuesUnique = require('./isValuesUnique');
var objectInterface = require('./objectInterface');
module.exports = {
substr: substr,
sortAndAddFirstElement: sortAndAddFirstElement,
dateDifferenceFromNow: dateDifferenceFromNow,
isValuesUnique: isValuesUnique,
objectInterface: objectInterface
};
+26
View File
@@ -0,0 +1,26 @@
var _uniqBy = require('lodash/uniqBy');
/**
* Checking if values are unique
*
* @customNeeds
* For e.g. [{email:'api@test.com'}, {email:'api@test.com'}] - email is not valid
*
* @since 1.3.0
* @category Array
*
* @param {Array} array - The array of objects
* @param {String} keyName - Name of the object property from an array in which unique will be checking
*
* @returns {Boolean} Returns true if values are unique and false if not
*
* @example
*
* famulus.isValuesUnique([{email:'api@test.com'}, {email:'api@test.com'}], 'email')
* // => false
*/
function isValuesUnique(array, keyName) {
return _uniqBy(array, keyName).length === array.length;
}
module.exports = isValuesUnique;
+51
View File
@@ -0,0 +1,51 @@
/**
* Interface for building an object by configuration
*
* @customNeeds - have an interface for building an object based on configuration
*
* @since 2.1.0
* @category Object
*
* @param {Array} config - Keys with configuration
* [
* 'key/value' - "OR" if no value, set value after "/",
* 'key|this.firstName + " " + this.lastName' - set value from the expression after "|" which is bind to the passed object,
* 'key:new Date()' - set value from the expression after ":"
* ]
*
* @returns {Function}
*
* @example
*
* var email = objectInterface(['body', 'count/1', 'sender|this.firstName + " " + this.lastName', 'isRead: false', 'created: new Date()'])
* // => function
*
* email({body: 'Hello world!', count: '', firstName: 'Vasyl', lastName: 'Stokolosa', another: ''})
* // => {body: "Hello world!", count: 1, created: Mon Jul 09 2018 10:31:08, isRead: false, sender: "Vasyl Stokolosa"}
*/
function objectInterface(config) {
return function(obj) {
var result = {};
for (var i = 0; i < config.length; i++) {
var OR, NEXT, REAL;
if ((OR = config[i].split('/')) && OR[1]) {
result[OR[0]] = obj[OR[0]] || Function('return ' + OR[1])();
}
else if ((NEXT = config[i].split('|')) && NEXT[1]) {
result[NEXT[0]] = Function('return ' + NEXT[1]).call(obj);
}
else if ((REAL = config[i].split(':')) && REAL[1]) {
result[REAL[0]] = Function('return ' + REAL[1])();
}
else {
result[config[i]] = obj[config[i]];
}
}
return result;
}
}
module.exports = objectInterface;
+31
View File
@@ -0,0 +1,31 @@
{
"name": "famulus",
"version": "2.2.2",
"description": "JavaScript library that provides a useful functional programming helpers. Add your own.",
"main": "famulus.js",
"repository": "https://github.com/shystruk/famulus.git",
"scripts": {
"report-coverage": "cat ./coverage/lcov.info | codecov",
"coverage": "nyc report --reporter=lcov",
"test": "nyc ava tests && npm run coverage"
},
"keywords": [
"utilities",
"helpers",
"helper"
],
"author": {
"name": "Vasyl Stokolosa",
"email": "v.stokol@gmail.com",
"url": "https://github.com/shystruk"
},
"license": "MIT",
"dependencies": {
"lodash": "^4.17.20"
},
"devDependencies": {
"ava": "^0.24.0",
"codecov.io": "^0.1.6",
"nyc": "^11.4.1"
}
}
@@ -0,0 +1,30 @@
var _ = require('lodash');
/**
* Sort an array by the name of existing property and add a first element into array
*
* @customNeeds Sort array by name and add first element.
* For e.g. user names - [ {name: 'All'}, {name: 'Aron'}, {name: 'Bob'} ]
*
* @since 1.1.0
* @category Array
*
* @param {Array} array - The array to sort and add
* @param {String} sortBy - Name of the property from an array by which array will be sorted
* @param {*} element - Element which is added into an array
*
* @returns {Array} Returns the new array
*
* @example
*
* famulus.sortAndAddFirstElement([{name:'Bob'}, {name:'Aron'}], 'name', {name:'All'})
* // => [ {name:'All'}, {name:'Aron'}, {name:'Bob'} ]
*/
function sortAndAddFirstElement(array, sortBy, element) {
return _(array)
.sortBy(sortBy)
.unshift(element)
.value();
}
module.exports = sortAndAddFirstElement;
+35
View File
@@ -0,0 +1,35 @@
var isString = require('lodash/isString');
/**
* Extracts parts of a string, beginning at the character at the specified position,
* and returns the specified number of characters.
* The substr() does not change the original string.
*
* @customNeeds Validate string type for preventing SyntaxError
*
* @since 1.0.0
* @category String
*
* @param {String} string - The string to extract
* @param {Number} start - The position where to start the extraction. First character is at index 0
* @param {Number?} length - Optional. The number of characters to extract. If omitted, it extracts the rest of the string
*
* @returns {String} Returns extract part of a string
*
* @example
*
* famulus.substr('Hello World!', 0, 5)
* // => 'Hello'
*
* famulus.substr('Hello World!', 6)
* // => 'World!'
*/
function substr(string, start, length) {
if (!isString(string)) {
return string;
}
return string.substr(start, length);
}
module.exports = substr;
@@ -0,0 +1,28 @@
import test from 'ava';
import dateDifference from '../dateDifference';
test('module should be a function', t => {
t.is(typeof dateDifference, 'function');
});
test('should return difference in days', t => {
t.is(dateDifference(new Date('06-20-2018'), new Date('06-26-2018'), 'days'), 6);
});
test('should return difference in hours', t => {
t.is(dateDifference(new Date('06-20-2018'), new Date('06-26-2018'), 'hours'), 144);
t.is(dateDifference(new Date('06-26-2018 10:00'), new Date('06-26-2018 08:00'), 'hours'), 2)
});
test('should return difference in minutes', t => {
t.is(dateDifference(new Date('06-20-2018'), new Date('06-26-2018'), 'minutes'), 8640);
});
test('should return difference in milliseconds', t => {
t.is(dateDifference(new Date('06-26-2018'), new Date('06-20-2018'), 'milliseconds'), 518400000);
});
test('should return all difference types in object', t => {
const diffResult = { days: 6, hours: 2, minutes: 10, milliseconds: 526200000 };
t.deepEqual(dateDifference(new Date('06-26-2018 10:10'), new Date('06-20-2018 08:00'), 'all'), diffResult);
});
@@ -0,0 +1,26 @@
import test from 'ava';
import dateDifferenceFromNow from '../dateDifferenceFromNow';
test('module should be a function', t => {
t.is(typeof dateDifferenceFromNow, 'function');
});
test('should return Number: days', t => {
t.is(typeof dateDifferenceFromNow(new Date('12-20-2017'), 'days'), 'number');
});
test('should return Number: hours', t => {
t.is(typeof dateDifferenceFromNow(new Date('12-20-2017'), 'hours'), 'number');
});
test('should return Number: minutes', t => {
t.is(typeof dateDifferenceFromNow(new Date('12-20-2017'), 'minutes'), 'number');
});
test('should return Number: milliseconds', t => {
t.is(typeof dateDifferenceFromNow(new Date('12-20-2017'), 'milliseconds'), 'number');
});
test('should return Object: all', t => {
t.deepEqual(typeof dateDifferenceFromNow(new Date('12-20-2017'), 'all'), 'object');
});
@@ -0,0 +1,17 @@
import test from 'ava';
import isValuesUnique from './../isValuesUnique';
const notUniqueData = [{email: 'api@test.com'}, {email: 'api@test.com'}];
const uniqueData = [{email: 'api@test.com'}, {email: 'api_1@test.com'}];
test('module should be a function', t => {
t.is(typeof isValuesUnique, 'function');
});
test('should return true when values are unique', t => {
t.is(isValuesUnique(uniqueData, 'email'), true);
});
test('should return false when values are not unique', t => {
t.is(isValuesUnique(notUniqueData, 'email'), false);
});
@@ -0,0 +1,19 @@
import test from 'ava';
import objectInterface from './../objectInterface';
const result = {body: "Hello world!", count: 1, sender: "Vasyl Stokolosa", isRead: false, created: 1531119600000};
test('module should be a function', t => {
t.is(typeof objectInterface, 'function');
});
test('should return object based on interface configuration', t => {
let email = objectInterface([
'body', 'count/1', 'sender|this.firstName + " " + this.lastName', 'created: Date.parse("Tue Jul 09 2018 17 GMT-0700")', 'isRead: false'
]);
t.deepEqual(email({
body: 'Hello world!', count: '', firstName: 'Vasyl', lastName: 'Stokolosa', another: ''
}), result
);
});
@@ -0,0 +1,13 @@
import test from 'ava';
import sortAndAddFirstElement from './../sortAndAddFirstElement';
test('module should be a function', t => {
t.is(typeof sortAndAddFirstElement, 'function');
});
test('should sort and add first element', t => {
t.deepEqual(
sortAndAddFirstElement([{name:'Bob'}, {name:'Aron'}], 'name', {name:'All'}),
[ {name:'All'}, {name:'Aron'}, {name:'Bob'} ]
);
});
+18
View File
@@ -0,0 +1,18 @@
import test from 'ava';
import substr from './../substr';
test('module should be a function', t => {
t.is(typeof substr, 'function');
});
test('should substring "Hello"', t => {
t.is(substr('Hello World!', 0, 5), 'Hello');
});
test('should substring "World!"', t => {
t.is(substr('Hello World!', 6), 'World!');
});
test('should return value if it is not correct', t => {
t.deepEqual(substr({}, 0, 5), {});
});