@bbn/bbn 1.0.347 → 1.0.349

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.
@@ -35,7 +35,8 @@
35
35
  * @param {Array} arr The subject array
36
36
  * @param {(String|Object|Function)} prop A property's name or a filter object or function
37
37
  * @param {*} val The value with which comparing the given property
38
- * @param {String} operator The operator to use for comparison with the value as used in bbn.fn.compare
38
+ * @param {Number|false|String} operator The operator to use for comparison with the value as used in bbn.fn.compare
39
+ * @param {Number|false} from The index to start from, if false the search starts from the end
39
40
  * @returns {Object|Boolean} The item if found, false otherwise
40
41
  */
41
- export default function getRow(arr: any[], prop: object | string, val?: any, operator?: string): any | false;
42
+ export default function getRow(arr: any[], prop: object | string, val?: any, operator?: number | false | string, from?: false | number): any | false;
@@ -36,13 +36,15 @@ import search from './search.js';
36
36
  * @param {Array} arr The subject array
37
37
  * @param {(String|Object|Function)} prop A property's name or a filter object or function
38
38
  * @param {*} val The value with which comparing the given property
39
- * @param {String} operator The operator to use for comparison with the value as used in bbn.fn.compare
39
+ * @param {Number|false|String} operator The operator to use for comparison with the value as used in bbn.fn.compare
40
+ * @param {Number|false} from The index to start from, if false the search starts from the end
40
41
  * @returns {Object|Boolean} The item if found, false otherwise
41
42
  */
42
- export default function getRow(arr, prop, val, operator) {
43
+ export default function getRow(arr, prop, val, operator, from) {
43
44
  if (val === void 0) { val = null; }
44
45
  if (operator === void 0) { operator = '='; }
45
- var idx = search(arr, prop, val, operator);
46
+ if (from === void 0) { from = 0; }
47
+ var idx = search(arr, prop, val, operator, from);
46
48
  if (idx > -1) {
47
49
  return arr[idx];
48
50
  }
@@ -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?: number): number;
100
+ export default function search(arr: any[], prop: Filter | object | string | ((a: any, i: string | number | symbol) => boolean), val?: any, operator?: number | false | string, startFrom?: number | false): number;
@@ -3,7 +3,6 @@ import compareConditions from './compareConditions.js';
3
3
  import { filterToConditions } from './filterToConditions.js';
4
4
  import isObject from '../type/isObject.js';
5
5
  import numProperties from './numProperties.js';
6
- import isNumber from '../type/isNumber.js';
7
6
  /**
8
7
  * Retrieves the index of the array's first element corresponding to the given filter.
9
8
  *
@@ -152,25 +151,45 @@ export default function search(arr, prop, val, operator, startFrom) {
152
151
  }
153
152
  }
154
153
  if (isFn || (isObject(filter) && numProperties(filter))) {
155
- if (isNumber(operator)) {
156
- startFrom = typeof (operator) === 'number' ? operator : 0;
154
+ var dir = 'asc';
155
+ if ((startFrom === undefined) && (typeof operator !== 'string')) {
156
+ startFrom = operator;
157
157
  operator = undefined;
158
158
  }
159
- if (!isNumber(startFrom)) {
160
- startFrom = 0;
159
+ if (startFrom === false) {
160
+ startFrom = arr.length - 1;
161
+ dir = 'desc';
161
162
  }
162
163
  if (typeof filter === 'function') {
163
- for (var i = startFrom; i < arr.length; i++) {
164
- if (filter(arr[i])) {
165
- return i;
164
+ if (dir === 'desc') {
165
+ for (var i = startFrom; i >= 0; i--) {
166
+ if (filter(arr[i])) {
167
+ return i;
168
+ }
169
+ }
170
+ }
171
+ else {
172
+ for (var i = startFrom; i < arr.length; i++) {
173
+ if (filter(arr[i])) {
174
+ return i;
175
+ }
166
176
  }
167
177
  }
168
178
  }
169
179
  else {
170
180
  filter = filterToConditions(filter);
171
- for (var i = startFrom; i < arr.length; i++) {
172
- if (compareConditions(arr[i], filter)) {
173
- return i;
181
+ if (dir === 'desc') {
182
+ for (var i = startFrom; i >= 0; i--) {
183
+ if (compareConditions(arr[i], filter)) {
184
+ return i;
185
+ }
186
+ }
187
+ }
188
+ else {
189
+ for (var i = startFrom; i < arr.length; i++) {
190
+ if (compareConditions(arr[i], filter)) {
191
+ return i;
192
+ }
174
193
  }
175
194
  }
176
195
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "1.0.347",
3
+ "version": "1.0.349",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",