New util extractProperty() and some tests.

This commit is contained in:
Julien Fontanet 2015-05-26 17:59:52 +02:00
parent 61731e2c2e
commit 8b03890f2a
2 changed files with 58 additions and 0 deletions

View File

@ -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)

49
src/utils.spec.js Normal file
View File

@ -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')
})
})