Add Blake2s & XXHash utility functions (#66)
* Initial split of util & util-crypto * Add initial XXHash version * Blake2s wit tests & documentation * Add sha512 (from NaCl) * Add randomBytes (from NaCl) * Flow fixes * Update documentation * Update latest @polkadot/dev
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const isNull = require('../is/null');
|
||||
const isUndefined = require('../is/undefined');
|
||||
|
||||
/**
|
||||
@name arrayFilter
|
||||
@signature arrayFilter (array: Array<mixed>, allowNulls: boolean = true): Array<mixed>
|
||||
@summary Filters undefined and (optionally) null values from an array
|
||||
@description
|
||||
Returns a new array with all `undefined` values removed. Optionally, when `allowNulls = false`, it removes the `null` values as well
|
||||
@example
|
||||
import { arrayFilter } from '@polkadot/util';
|
||||
|
||||
arrayFilter([0, void 0, true, null, false, '']); // [0, true, null, false, '']
|
||||
arrayFilter([0, void 0, true, null, false, ''], false); // [0, true, false, '']
|
||||
*/
|
||||
module.exports = function arrayFilter (array: Array<mixed>, allowNulls: boolean = true): Array<mixed> {
|
||||
return array.filter((value) => {
|
||||
return !isUndefined(value) && (allowNulls || !isNull(value));
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { arrayFilter } = require('./index');
|
||||
|
||||
describe('filterArray', () => {
|
||||
it('filters arrays, removing undefined', () => {
|
||||
expect(
|
||||
arrayFilter([0, '', null, false, void 0, NaN])
|
||||
).toEqual([0, '', null, false, NaN]);
|
||||
});
|
||||
|
||||
it('filters arrays, removing undefined & null (allowNull = false)', () => {
|
||||
expect(
|
||||
arrayFilter([0, '', null, false, void 0, NaN], false)
|
||||
).toEqual([0, '', false, NaN]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,13 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const arrayFilter = require('./filter');
|
||||
|
||||
/**
|
||||
@summary Utility methods that operates on arrays
|
||||
*/
|
||||
module.exports = {
|
||||
arrayFilter
|
||||
};
|
||||
Reference in New Issue
Block a user