@bbn/bbn 1.0.374 → 1.0.375

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -97,4 +97,4 @@ import { Filter } from './filterToConditions.js';
97
97
  * @param {Number} startFrom The index from which the search should start
98
98
  * @returns {Number} The index if found, otherwise -1
99
99
  */
100
- export default function search(arr: any[], prop: Filter | object | string | ((a: any, i: string | number | symbol) => boolean), val?: any, operator?: number | string, startFrom?: boolean | number, backward?: boolean): number;
100
+ export default function search(arr: any[], prop: Filter | object | string | ((a: any, i: string | number | symbol) => boolean), val?: any, operator?: number | string | boolean, startFrom?: boolean | number, backward?: boolean): number;
@@ -2,8 +2,7 @@ import isIterable from '../type/isIterable.js';
2
2
  import compareConditions from './compareConditions.js';
3
3
  import { filterToConditions } from './filterToConditions.js';
4
4
  import isObject from '../type/isObject.js';
5
- import numProperties from './numProperties.js';
6
- import isNumber from '../type/isNumber.js';
5
+ import isArray from '../type/isArray.js';
7
6
  /**
8
7
  * Retrieves the index of the array's first element corresponding to the given filter.
9
8
  *
@@ -115,6 +114,9 @@ export default function search(arr, prop, val, operator, startFrom, backward) {
115
114
  }
116
115
  var filter;
117
116
  var isFn = false;
117
+ if (!prop) {
118
+ throw new Error(bbn._('The second argument for a search cannot be null or undefined'));
119
+ }
118
120
  if (typeof prop === 'string') {
119
121
  filter = {
120
122
  conditions: [
@@ -126,38 +128,32 @@ export default function search(arr, prop, val, operator, startFrom, backward) {
126
128
  ]
127
129
  };
128
130
  }
129
- else if (!prop) {
130
- isFn = true;
131
- filter = function (a) {
132
- return compareConditions({ value: a }, filterToConditions({
133
- logic: 'AND',
134
- conditions: [
135
- {
136
- field: 'value',
137
- operator: operator || '=',
138
- value: val,
139
- },
140
- ],
141
- }));
142
- };
143
- }
144
131
  else {
145
- backward = startFrom === true ? true : false;
146
- startFrom = typeof (operator) === 'number' ? operator : (backward ? arr.length - 1 : 0);
147
- operator = val;
148
- if (isObject(prop)) {
149
- filter = prop;
150
- }
151
- else if (typeof (prop) === 'function') {
132
+ backward = operator === true ? true : false;
133
+ startFrom = typeof (val) === 'number' ? val : (backward ? arr.length - 1 : 0);
134
+ if (typeof prop === 'function') {
152
135
  isFn = true;
153
136
  filter = prop;
154
137
  }
155
- }
156
- if (isFn || (isObject(filter) && numProperties(filter))) {
157
- if (isNumber(operator)) {
158
- startFrom = typeof (operator) === 'number' ? operator : 0;
159
- operator = undefined;
138
+ else if (isObject(prop)) {
139
+ if (!('conditions' in prop)) {
140
+ filter = {
141
+ conditions: [prop],
142
+ logic: 'AND'
143
+ };
144
+ }
145
+ else {
146
+ filter = prop;
147
+ }
160
148
  }
149
+ else if (isArray(prop)) {
150
+ filter = {
151
+ conditions: prop,
152
+ logic: 'AND'
153
+ };
154
+ }
155
+ }
156
+ if (isFn || filter) {
161
157
  if (typeof startFrom !== 'number') {
162
158
  startFrom = backward ? arr.length - 1 : 0;
163
159
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "1.0.374",
3
+ "version": "1.0.375",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",