Skip to content

Commit 31a71b5

Browse files
Added contains()
1 parent e63692b commit 31a71b5

File tree

4 files changed

+89
-11
lines changed

4 files changed

+89
-11
lines changed

index.cjs.js

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Object.defineProperty(exports, "__esModule", {
44
value: true
55
});
6-
exports.sortBy = exports.Objectify = exports.any = exports.all = exports.removeAt = exports.insertAt = exports.range = exports.shuffle = exports.default = void 0;
6+
exports.contains = exports.sortBy = exports.Objectify = exports.any = exports.all = exports.removeAt = exports.insertAt = exports.range = exports.shuffle = exports.default = void 0;
77

88
var _locustjsBase = require("locustjs-base");
99

@@ -238,6 +238,40 @@ var sortBy = function sortBy(arr) {
238238

239239
exports.sortBy = sortBy;
240240

241+
var contains = function contains(arr) {
242+
var result = [];
243+
244+
for (var _len2 = arguments.length, values = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
245+
values[_key2 - 1] = arguments[_key2];
246+
}
247+
248+
for (var i = 0; i < values.length; i++) {
249+
if ((0, _locustjsBase.isPrimitive)(values[i])) {
250+
values[i] = values[i].toString().toLowerCase();
251+
}
252+
}
253+
254+
for (var _i = 0; _i < arr.length; _i++) {
255+
if ((0, _locustjsBase.isPrimitive)(arr[_i])) {
256+
for (var j = 0; j < values.length; j++) {
257+
if (arr[_i].toString().toLowerCase() == values[j]) {
258+
result.push(true);
259+
}
260+
}
261+
} else {
262+
for (var _j = 0; _j < values.length; _j++) {
263+
if ((0, _locustjsBase.equals)(arr[_i], values[_j])) {
264+
result.push(true);
265+
}
266+
}
267+
}
268+
}
269+
270+
return result.length == values.length;
271+
};
272+
273+
exports.contains = contains;
274+
241275
function configureArrayExtensions(options) {
242276
var _options = (0, _locustjsExtensionsOptions.configureOptions)(options);
243277

@@ -282,6 +316,16 @@ function configureArrayExtensions(options) {
282316
};
283317
}
284318

319+
if (!Array.prototype.contains || (0, _locustjsExtensionsOptions.shouldExtend)('contains', _options)) {
320+
Array.prototype.contains = function () {
321+
for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
322+
args[_key3] = arguments[_key3];
323+
}
324+
325+
return contains.apply(void 0, [this].concat(args));
326+
};
327+
}
328+
285329
if (!Array.prototype.Objectify || (0, _locustjsExtensionsOptions.shouldExtend)('Objectify', _options)) {
286330
/* this method has close relation with String.prototype.nestedSplit in locustjs-extensions-string
287331
examples
@@ -314,8 +358,8 @@ function configureArrayExtensions(options) {
314358

315359
if (!Array.prototype.sortBy || (0, _locustjsExtensionsOptions.shouldExtend)('sortBy', _options)) {
316360
Array.prototype.sortBy = function () {
317-
for (var _len2 = arguments.length, fns = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
318-
fns[_key2] = arguments[_key2];
361+
for (var _len4 = arguments.length, fns = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
362+
fns[_key4] = arguments[_key4];
319363
}
320364

321365
return sortBy.apply(void 0, [this].concat(fns));

index.esm.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isArray, isFunction } from 'locustjs-base'
1+
import { isArray, isFunction, isPrimitive, equals } from 'locustjs-base'
22
import { configureOptions, shouldExtend } from 'locustjs-extensions-options'
33
import { deepAssign } from 'locustjs-extensions-object'
44

@@ -195,6 +195,33 @@ const sortBy = function (arr, ...fns) {
195195

196196
return arr.sort(sort_fn);
197197
}
198+
const contains = function (arr, ...values) {
199+
let result = [];
200+
201+
for (let i = 0; i < values.length; i++) {
202+
if (isPrimitive(values[i])) {
203+
values[i] = values[i].toString().toLowerCase();
204+
}
205+
}
206+
207+
for (let i = 0; i < arr.length; i++) {
208+
if (isPrimitive(arr[i])) {
209+
for (let j = 0; j < values.length; j++) {
210+
if (arr[i].toString().toLowerCase() == values[j] ) {
211+
result.push(true);
212+
}
213+
}
214+
} else {
215+
for (let j = 0; j < values.length; j++) {
216+
if (equals(arr[i], values[j])) {
217+
result.push(true);
218+
}
219+
}
220+
}
221+
}
222+
223+
return result.length == values.length;
224+
}
198225

199226
function configureArrayExtensions(options) {
200227
const _options = configureOptions(options)
@@ -240,6 +267,12 @@ function configureArrayExtensions(options) {
240267
}
241268
}
242269

270+
if (!Array.prototype.contains || shouldExtend('contains', _options)) {
271+
Array.prototype.contains = function (...args) {
272+
return contains(this, ...args);
273+
}
274+
}
275+
243276
if (!Array.prototype.Objectify || shouldExtend('Objectify', _options)) {
244277
/* this method has close relation with String.prototype.nestedSplit in locustjs-extensions-string
245278
examples
@@ -287,5 +320,6 @@ export {
287320
all,
288321
any,
289322
Objectify,
290-
sortBy
323+
sortBy,
324+
contains
291325
}

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "locustjs-extensions-array",
3-
"version": "1.0.10",
3+
"version": "1.0.11",
44
"description": "This library contains extensions for Array",
55
"main": "index.cjs.js",
66
"module": "index.esm.js",
@@ -23,7 +23,7 @@
2323
},
2424
"homepage": "https://github.com/ironcodev/locustjs-extensions-array#readme",
2525
"dependencies": {
26-
"locustjs-base": "^1.0.4",
26+
"locustjs-base": "^1.0.7",
2727
"locustjs-extensions-object": "^1.0.8",
2828
"locustjs-extensions-options": "^1.0.5"
2929
}

0 commit comments

Comments
 (0)