From 8b03890f2a05c7385a63cd2c74f34572cd64c1d0 Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Tue, 26 May 2015 17:59:52 +0200 Subject: [PATCH] New util extractProperty() and some tests. --- src/utils.js | 9 +++++++++ src/utils.spec.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/utils.spec.js diff --git a/src/utils.js b/src/utils.js index a1b939c9b..07171b690 100644 --- a/src/utils.js +++ b/src/utils.js @@ -21,6 +21,15 @@ export const ensureArray = (value) => { // ------------------------------------------------------------------- +// Returns the value of a property and removes it from the object. +export function extractProperty (obj, prop) { + const value = obj[prop] + delete obj[prop] + return value +} + +// ------------------------------------------------------------------- + // Generate a secure random Base64 string. export const generateToken = (function (randomBytes) { return (n = 32) => randomBytes(n).then(base64url) diff --git a/src/utils.spec.js b/src/utils.spec.js new file mode 100644 index 000000000..fb148c7e2 --- /dev/null +++ b/src/utils.spec.js @@ -0,0 +1,49 @@ +/* eslint-env mocha */ + +import {expect} from 'chai' + +// =================================================================== + +import { + ensureArray, + extractProperty +} from './utils' + +// =================================================================== + +describe('ensureArray', function () { + it('returns an empty array for undefined', function () { + expect(ensureArray(undefined)).to.eql([]) + }) + + it('returns the object itself if is already an array', function () { + const array = ['foo', 'bar', 'baz'] + + expect(ensureArray(array)).to.equal(array) + }) + + it('wrap the value in an object', function () { + const value = {} + + expect(ensureArray(value)).to.includes(value) + }) +}) + +// ------------------------------------------------------------------- + +describe('extractProperty', function () { + it('returns the value of the property', function () { + const value = {} + const obj = { prop: value } + + expect(extractProperty(obj, 'prop')).to.equal(value) + }) + + it('removes the property from the object', function () { + const value = {} + const obj = { prop: value } + + expect(extractProperty(obj, 'prop')).to.equal(value) + expect(obj).to.not.have.property('prop') + }) +})