Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

feat(helpers): add helpers module #212

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions src/helpers.ats
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Convert string to dash case
*
* @param {string} str
* @returns {string}
*/
export function dashCase(str: string) {
return str.replace(/([A-Z])/g, function ($1) {
return '-' + $1.toLowerCase();
});
}

/**
* Create deep copy of an object
*
* @param {Object} obj
* @returns {Object} deep copy
*/
export function copy(obj) {
return JSON.parse(JSON.stringify(obj));
}

/**
* Check if no instruction was matched
*
* @param {Object} instruction
* @returns {boolean}
*/
export function notMatched(instruction) {
return instruction == null || instruction.length < 1;
}

/**
* Call function with each key/value pair of an object's properties
*
* @param {Object} obj
* @param {function} fn
* @returns undefined
*/
export function forEach(obj, fn) {
Object.keys(obj).forEach(key => fn(obj[key], key));
}

/**
* Creates a new array with the results of calling a provided function
* on every key/value pair in the object's properties
*
* @param {Object} obj
* @param {function} fn
* @returns {Array}
*/
export function mapObj(obj, fn) {
var result = [];
Object.keys(obj).forEach(key => result.push(fn(obj[key], key)));
return result;
}

/**
* Convert boolean to promise
*
* @param value
* @returns {Object} promise
*/
export function boolToPromise (value) {
return value ? Promise.resolve(value) : Promise.reject();
}

134 changes: 134 additions & 0 deletions test/helpers.spec.ats
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import * as helpers from '../src/helpers';

describe('helpers', () => {

describe('dashCase', () => {

it('should dasherize camelCase strings correctly', () => {
expect(helpers.dashCase('camelCaseString')).toEqual('camel-case-string');
});

it('should not change numbers', () => {
expect(helpers.dashCase('0123456789')).toEqual('0123456789');
});

it('should not change special characters', () => {
expect(helpers.dashCase('_-.')).toEqual('_-.');
});

it('should not change lowercase characters abcdefghijklmnopqrstuvwxyz', () => {
expect(helpers.dashCase('abcdefghijklmnopqrstuvwxyz')).toEqual('abcdefghijklmnopqrstuvwxyz');
});

it('should dasherize all uppercase characters ABCDEFGHIJKLMNOPQRSTUVWXYZ', () => {
expect(helpers.dashCase('ABCDEFGHIJKLMNOPQRSTUVWXYZ')).toEqual('-a-b-c-d-e-f-g-h-i-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z');
});

});

describe('copy', () => {

it('should return a deep copy of an object', () => {
var obj = {
one: "1",
two: "2",
three: {
four: [5, 6]
}
};
expect(helpers.copy(obj)).toEqual(obj);
expect(helpers.copy(obj)).not.toBe(obj);
});

});

describe('notMatched', () => {

it('should return true if null value is passed', () => {
expect(helpers.notMatched()).toEqual(true);
expect(helpers.notMatched('')).toEqual(true);
expect(helpers.notMatched(undefined)).toEqual(true);
});

it('should return true if empty array is passed', () => {
expect(helpers.notMatched([])).toEqual(true);
});

it('should return false if non-empty array is passed', () => {
expect(helpers.notMatched([{}])).toEqual(false);
});

it('should return false if object is passed', () => {
expect(helpers.notMatched({})).toEqual(false);
});

});

describe('forEach', () => {

it('should call a function with each key/value pair of an object', () => {
var obj = {
one: "1",
two: "2",
three: {
four: [5, 6]
}
};
var fn = jasmine.createSpy('fn');
helpers.forEach(obj, fn);
expect(fn).toHaveBeenCalledWith("1", "one");
expect(fn).toHaveBeenCalledWith("2", "two");
expect(fn).toHaveBeenCalledWith({
four: [5, 6]
}, "three");
});

});

describe('mapObj', () => {

it('should apply a function to each key/value pair of an object', () => {
var obj = {
one: "1",
two: "2",
three: {
four: [5, 6]
}
};
var fn = function(value, key){
return {
value: value,
key: key
};
}
var output = helpers.mapObj(obj, fn);
expect(output).toEqual([
{ value: obj['one'], key: 'one'},
{ value: obj['two'], key: 'two'},
{ value: obj['three'], key: 'three'}
])
});

});

describe('boolToPromise', () => {

it('should return a resolved promise for truthy arguments', (done) => {
helpers.boolToPromise(true).then(done, null);
});

it('should return a rejected promise for falsy arguments', (done) => {
helpers.boolToPromise(false).then(null, done);
});

it('should pass the original argument to the success handler if truthy', (done) => {
var obj = { one: '1' };
helpers.boolToPromise(obj).then(function(value){
expect(value).toBe(obj);
done();
}, null);
});

});

});