From 6468e751acafff7e4661be35804b8283de4e36d9 Mon Sep 17 00:00:00 2001 From: Jurgen Van de Moere Date: Fri, 27 Mar 2015 00:18:51 +0100 Subject: [PATCH 1/2] feat(helpers): add helpers module --- src/helpers.ats | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/helpers.ats diff --git a/src/helpers.ats b/src/helpers.ats new file mode 100644 index 0000000..d6bd3a2 --- /dev/null +++ b/src/helpers.ats @@ -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(); +} + From da899cc7273fc7b03f2189cfd094de38d015f81e Mon Sep 17 00:00:00 2001 From: Jurgen Van de Moere Date: Fri, 27 Mar 2015 00:19:08 +0100 Subject: [PATCH 2/2] test(helpers): add unit tests for helpers module --- test/helpers.spec.ats | 134 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 test/helpers.spec.ats diff --git a/test/helpers.spec.ats b/test/helpers.spec.ats new file mode 100644 index 0000000..4d98e72 --- /dev/null +++ b/test/helpers.spec.ats @@ -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); + }); + + }); + +});