Skip to content

Commit

Permalink
Merge 69be56d into 3fa32f6
Browse files Browse the repository at this point in the history
  • Loading branch information
XhmikosR committed Apr 21, 2022
2 parents 3fa32f6 + 69be56d commit a0813bc
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
4 changes: 2 additions & 2 deletions bin/svg-sprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const yaml = require('js-yaml');
const glob = require('glob');
let yargs = require('yargs');
const SVGSpriter = require('../lib/svg-sprite.js');
const { isObject } = require('../lib/svg-sprite/utils/index.js');
const { isObject, zipObject } = require('../lib/svg-sprite/utils/index.js');

yargs
.usage('Create one or multiple sprites of the given SVG files, optionally along with some stylesheet resources.\nUsage: $0 [options] files')
Expand Down Expand Up @@ -210,7 +210,7 @@ if (typeof config.shape.transform === 'string') {
const transformConfigFile = argv[`shape-transform-${transform}`];
const transformConfigJSON = fs.readFileSync(path.resolve(transformConfigFile), 'utf8');
const transformConfig = transformConfigJSON.trim() ? JSON.parse(transformConfigJSON) : {};
this.push(_.zipObject([transform], [transformConfig]));
this.push(zipObject([transform], [transformConfig]));
} catch {}
} else {
this.push(transform);
Expand Down
4 changes: 2 additions & 2 deletions lib/svg-sprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const Config = require('./svg-sprite/config.js');
const Queue = require('./svg-sprite/queue.js');
const Layouter = require('./svg-sprite/layouter.js');
const svgo = require('./svg-sprite/transform/svgo.js');
const { isFunction, isObject, isPlainObject } = require('./svg-sprite/utils/index.js');
const { isFunction, isObject, isPlainObject, zipObject } = require('./svg-sprite/utils/index.js');

// TODO: after Node.js 12.x, `os.cpus` should never be undefined
const CPU_COUNT = os.cpus() && os.cpus().length;
Expand Down Expand Up @@ -330,7 +330,7 @@ SVGSpriter.prototype._layout = function(config, cb) {
}

async.parallelLimit(tasks, this._limit, (error, data) => {
cb(error, files, _.zipObject(Object.keys(data).map(key => data[key].key), data));
cb(error, files, zipObject(Object.keys(data).map(key => data[key].key), data));
});
};

Expand Down
16 changes: 15 additions & 1 deletion lib/svg-sprite/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,23 @@ function isString(value) {
return Object.prototype.toString.call(value) === '[object String]';
}

/**
* @param {Array} array1 First array
* @param {Array} array2 Second array
* @returns {object} The zipped Object
*/
function zipObject(array1, array2) {
if (!(Array.isArray(array1) || Array.isArray(array2))) {
throw new TypeError('Some of passed parameters are not array');
}

return Object.fromEntries(array1.map((_, i) => ([array1[i], array2[i]])));
}

module.exports = {
isFunction,
isObject,
isPlainObject,
isString
isString,
zipObject
};
29 changes: 28 additions & 1 deletion test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

/* eslint-disable unicorn/new-for-builtins, no-new-wrappers, prefer-regex-literals, jest/prefer-expect-assertions */

const { isFunction, isObject, isString, isPlainObject } = require('../lib/svg-sprite/utils/index.js');
const {
isFunction,
isObject,
isString,
isPlainObject,
zipObject
} = require('../lib/svg-sprite/utils/index.js');

describe('utils', () => {
describe('isFunction', () => {
Expand Down Expand Up @@ -193,4 +199,25 @@ describe('utils', () => {
expect(isPlainObject(Symbol('test'))).toBe(false);
});
});

describe('zipObject', () => {
it('should return the zipped object', () => {
expect(zipObject(['a', 'b'], [1, 2])).toStrictEqual({ a: 1, b: 2 });
});

it('should return the zipped object with imbalanced arrays', () => {
expect(zipObject(['a', 'b', 'c'], [1, 2])).toStrictEqual({ a: 1, b: 2, c: undefined });
expect(zipObject(['a'], [1, 2])).toStrictEqual({ a: 1 });
});

it('should not fail with empty arrays', () => {
expect(zipObject([], [])).toStrictEqual({});
});

it('should throw error if non-array value passed', () => {
expect(() => {
zipObject(1, false);
}).toThrow(new Error('Some of passed parameters are not array'));
});
});
});

0 comments on commit a0813bc

Please sign in to comment.