New util extractProperty() and some tests.
This commit is contained in:
parent
61731e2c2e
commit
8b03890f2a
@ -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
49
src/utils.spec.js
Normal 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')
|
||||
})
|
||||
})
|
Loading…
Reference in New Issue
Block a user