chore(xo-server/ensureArray): move into own module

This commit is contained in:
Julien Fontanet
2019-03-25 15:58:46 +01:00
parent f64917ec52
commit 478726fa3b
9 changed files with 32 additions and 44 deletions

View 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]

View 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)
})
})

View File

@@ -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'
// ===================================================================

View File

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

View File

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

View File

@@ -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 = {}

View File

@@ -1,7 +1,7 @@
import { startsWith } from 'lodash'
import ensureArray from './_ensureArray'
import {
ensureArray,
extractProperty,
forEach,
isArray,

View File

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

View File

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