Skip to content

Commit

Permalink
Merge pull request #1211 from MrDroid1/issue-750
Browse files Browse the repository at this point in the history
Remove usage of _.uniq
  • Loading branch information
icebob authored Jul 17, 2023
2 parents 7f22077 + 6ec67d9 commit cdd4933
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 17 deletions.
12 changes: 5 additions & 7 deletions examples/multi-nodes/node-controller.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const _ = require("lodash");
const kleur = require("kleur");
const cluster = require("cluster");
const { humanize, randomInt } = require("../../src/utils");
const { humanize, uniq, randomInt } = require("../../src/utils");

const padS = _.padStart;
const padE = _.padEnd;
Expand Down Expand Up @@ -270,12 +270,10 @@ module.exports = {
);
console.log(kleur.yellow().bold("========"));

const nodeIDs = _.uniq(
[].concat(
Object.keys(this.workerRegistry),
this.broker.registry.nodes.toArray().map(node => node.id)
)
)
const nodeIDs = uniq([
...Object.keys(this.workerRegistry),
...this.broker.registry.nodes.toArray().map(node => node.id)
])
.filter(nodeID => nodeID != this.broker.nodeID)
.sort((a, b) => Number(a.replace(/[^\d]/g, "")) - Number(b.replace(/[^\d]/g, "")));

Expand Down
9 changes: 5 additions & 4 deletions src/middlewares/hot-reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const path = require("path");
const watch = require("recursive-watch");
const _ = require("lodash");

const { clearRequireCache, makeDirs, isFunction, isString } = require("../utils");
const { clearRequireCache, makeDirs, isFunction, isString, uniq } = require("../utils");

/* istanbul ignore next */
module.exports = function HotReloadMiddleware(broker) {
Expand Down Expand Up @@ -250,10 +250,11 @@ module.exports = function HotReloadMiddleware(broker) {
if (service) {
// It is a service dependency. We should reload this service if this file has changed.
const watchItem = getWatchItem(fName);
if (!watchItem.services.includes(service.fullName))
if (!watchItem.services.includes(service.fullName)) {
watchItem.services.push(service.fullName);
}

watchItem.others = _.uniq([].concat(watchItem.others, parents || []));
watchItem.others = uniq([...watchItem.others, ...(parents || [])]);
} else if (isMoleculerConfig(fName)) {
const watchItem = getWatchItem(fName);
watchItem.brokerRestart = true;
Expand All @@ -262,7 +263,7 @@ module.exports = function HotReloadMiddleware(broker) {
if (parents) {
const watchItem = getWatchItem(fName);
watchItem.brokerRestart = true;
watchItem.others = _.uniq([].concat(watchItem.others, parents || []));
watchItem.others = uniq([...watchItem.others, ...(parents || [])]);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/registry/event-catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class EventCatalog {
* @memberof EventCatalog
*/
getGroups(eventName) {
return _.uniq(
return utils.uniq(
this.events.filter(list => utils.match(eventName, list.name)).map(item => item.group)
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/runner-esm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ export default class MoleculerRunner {
});

await Promise.all(
_.uniq(serviceFiles).map(async f => {
utils.uniq(serviceFiles).map(async f => {
const mod = await import(f.startsWith("/") ? f : "/" + f);
const content = mod.default;

Expand Down
2 changes: 1 addition & 1 deletion src/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ class MoleculerRunner {
}
});

_.uniq(serviceFiles).forEach(f => this.broker.loadService(f));
utils.uniq(serviceFiles).forEach(f => this.broker.loadService(f));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/service-broker.js
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,7 @@ class ServiceBroker {
) {
if (!Array.isArray(serviceNames)) serviceNames = [serviceNames];

serviceNames = _.uniq(
serviceNames = utils.uniq(
_.compact(
serviceNames.map(x => {
if (utils.isPlainObject(x) && x.name) {
Expand Down
4 changes: 2 additions & 2 deletions src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

const _ = require("lodash");
const { ServiceSchemaError, MoleculerError } = require("./errors");
const { isObject, isFunction, flatten, functionArguments, deprecate } = require("./utils");
const { isObject, isFunction, flatten, functionArguments, deprecate, uniq } = require("./utils");

/**
* Wrap a handler Function to an object with a `handler` property.
Expand Down Expand Up @@ -662,7 +662,7 @@ class Service {
const targetSS = target && target.$secureSettings ? target.$secureSettings : [];
if (!target) target = {};

target.$secureSettings = _.uniq([].concat(srcSS, targetSS));
target.$secureSettings = uniq([...srcSS, ...targetSS]);
}

return _.defaultsDeep(src, target);
Expand Down
10 changes: 10 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,16 @@ const utils = {
: [];
},

/**
* Creates a duplicate-free version of an array
*
* @param {Array<String|Number>} arr
* @returns {Array<String|Number>}
*/
uniq(arr) {
return [...new Set(arr)];
},

/**
* Produces a random floating number between the inclusive lower and upper bounds.
*
Expand Down
3 changes: 3 additions & 0 deletions test/unit/service-broker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ jest.mock("../../src/utils", () => ({
return ["ctx"];
},
deprecate() {},
uniq(arr) {
return [...new Set(arr)];
},
randomInt() {
return 2;
}
Expand Down
11 changes: 11 additions & 0 deletions test/unit/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -809,3 +809,14 @@ describe("Test utils.functionArguments", () => {
expect(utils.functionArguments(async function (alpha, beta) {})).toEqual(["alpha", "beta"]);
});
});

describe("Test utils.uniq", () => {
it("should return duplicate-free version of an array", () => {
expect(utils.uniq([1, 2, 3, 3])).toEqual([1, 2, 3]);
expect(utils.uniq(["one", "one", "two", "three"])).toEqual(["one", "two", "three"]);

expect(utils.uniq([null, null, undefined, undefined])).toEqual([null, undefined]);
expect(utils.uniq([null, null])).toEqual([null]);
expect(utils.uniq([undefined, undefined])).toEqual([undefined]);
});
});

0 comments on commit cdd4933

Please sign in to comment.