chore(xo-server/ensureArray): move into own module
This commit is contained in:
3
packages/xo-server/src/_ensureArray.js
Normal file
3
packages/xo-server/src/_ensureArray.js
Normal file
@@ -0,0 +1,3 @@
|
||||
// Ensure the value is an array, wrap it if necessary.
|
||||
export default value =>
|
||||
value === undefined ? [] : Array.isArray(value) ? value : [value]
|
||||
21
packages/xo-server/src/_ensureArray.spec.js
Normal file
21
packages/xo-server/src/_ensureArray.spec.js
Normal file
@@ -0,0 +1,21 @@
|
||||
/* eslint-env jest */
|
||||
|
||||
import ensureArray from './_ensureArray'
|
||||
|
||||
describe('ensureArray()', function() {
|
||||
it('wrap the value in an array', function() {
|
||||
const value = 'foo'
|
||||
|
||||
expect(ensureArray(value)).toEqual([value])
|
||||
})
|
||||
|
||||
it('returns an empty array for undefined', function() {
|
||||
expect(ensureArray(undefined)).toEqual([])
|
||||
})
|
||||
|
||||
it('returns the object itself if is already an array', function() {
|
||||
const array = ['foo', 'bar', 'baz']
|
||||
|
||||
expect(ensureArray(array)).toBe(array)
|
||||
})
|
||||
})
|
||||
@@ -1,8 +1,9 @@
|
||||
import asyncMap from '@xen-orchestra/async-map'
|
||||
import { some } from 'lodash'
|
||||
|
||||
import ensureArray from '../_ensureArray'
|
||||
import { asInteger } from '../xapi/utils'
|
||||
import { ensureArray, forEach, parseXml } from '../utils'
|
||||
import { forEach, parseXml } from '../utils'
|
||||
|
||||
// ===================================================================
|
||||
|
||||
|
||||
@@ -10,8 +10,9 @@ import { invalidParameters } from 'xo-common/api-errors'
|
||||
import { v4 as generateUuid } from 'uuid'
|
||||
import { includes, remove, filter, find, range } from 'lodash'
|
||||
|
||||
import ensureArray from '../_ensureArray'
|
||||
import { asInteger } from '../xapi/utils'
|
||||
import { parseXml, ensureArray } from '../utils'
|
||||
import { parseXml } from '../utils'
|
||||
|
||||
const log = createLogger('xo:xosan')
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ import forEach from 'lodash/forEach'
|
||||
import has from 'lodash/has'
|
||||
import highland from 'highland'
|
||||
import humanFormat from 'human-format'
|
||||
import isArray from 'lodash/isArray'
|
||||
import isString from 'lodash/isString'
|
||||
import keys from 'lodash/keys'
|
||||
import multiKeyHashInt from 'multikey-hash'
|
||||
@@ -49,17 +48,6 @@ export const diffItems = (coll1, coll2) => {
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
// Ensure the value is an array, wrap it if necessary.
|
||||
export function ensureArray(value) {
|
||||
if (value === undefined) {
|
||||
return []
|
||||
}
|
||||
|
||||
return isArray(value) ? value : [value]
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
// Returns the value of a property and removes it from the object.
|
||||
export function extractProperty(obj, prop) {
|
||||
const value = obj[prop]
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
import {
|
||||
camelToSnakeCase,
|
||||
diffItems,
|
||||
ensureArray,
|
||||
extractProperty,
|
||||
formatXml,
|
||||
generateToken,
|
||||
@@ -42,26 +41,6 @@ describe('diffItems', () => {
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
describe('ensureArray()', function() {
|
||||
it('wrap the value in an array', function() {
|
||||
const value = 'foo'
|
||||
|
||||
expect(ensureArray(value)).toEqual([value])
|
||||
})
|
||||
|
||||
it('returns an empty array for undefined', function() {
|
||||
expect(ensureArray(undefined)).toEqual([])
|
||||
})
|
||||
|
||||
it('returns the object itself if is already an array', function() {
|
||||
const array = ['foo', 'bar', 'baz']
|
||||
|
||||
expect(ensureArray(array)).toBe(array)
|
||||
})
|
||||
})
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
describe('extractProperty()', function() {
|
||||
it('returns the value of the property', function() {
|
||||
const value = {}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { startsWith } from 'lodash'
|
||||
|
||||
import ensureArray from './_ensureArray'
|
||||
import {
|
||||
ensureArray,
|
||||
extractProperty,
|
||||
forEach,
|
||||
isArray,
|
||||
|
||||
@@ -35,11 +35,11 @@ import {
|
||||
import { satisfies as versionSatisfies } from 'semver'
|
||||
|
||||
import createSizeStream from '../size-stream'
|
||||
import ensureArray from '../_ensureArray'
|
||||
import fatfsBuffer, { init as fatfsBufferInit } from '../fatfs-buffer'
|
||||
import pRetry from '../_pRetry'
|
||||
import {
|
||||
camelToSnakeCase,
|
||||
ensureArray,
|
||||
forEach,
|
||||
isFunction,
|
||||
map,
|
||||
|
||||
@@ -12,14 +12,9 @@ import sortBy from 'lodash/sortBy'
|
||||
import assign from 'lodash/assign'
|
||||
import unzip from 'julien-f-unzip'
|
||||
|
||||
import ensureArray from '../../_ensureArray'
|
||||
import { debounce } from '../../decorators'
|
||||
import {
|
||||
ensureArray,
|
||||
forEach,
|
||||
mapFilter,
|
||||
mapToArray,
|
||||
parseXml,
|
||||
} from '../../utils'
|
||||
import { forEach, mapFilter, mapToArray, parseXml } from '../../utils'
|
||||
|
||||
import { extractOpaqueRef, useUpdateSystem } from '../utils'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user