@bbn/bbn 1.0.433 → 1.0.436

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.
@@ -1,5 +1,5 @@
1
1
  import each from '../loop/each.js';
2
- import filter from '../object/filter.js';
2
+ import _filter from '../object/_filter.js';
3
3
  /**
4
4
  * Aborts (client side) all the XHR using the given URL if it still exists.
5
5
  *
@@ -21,7 +21,7 @@ import filter from '../object/filter.js';
21
21
  * @returns {undefined}
22
22
  */
23
23
  export default function abortURL(url) {
24
- each(filter(bbn.env.loaders, { url: url }), function (a) {
24
+ each(_filter(bbn.env.loaders, { url: url }), function (a) {
25
25
  if (a && a.source) {
26
26
  a.source.cancel('Operation canceled by the user.');
27
27
  }
@@ -1,5 +1,4 @@
1
1
  import substr from '../string/substr.js';
2
- import filter from '../object/filter.js';
3
2
  import extend from '../object/extend.js';
4
3
  import html2text from '../html/html2text.js';
5
4
  /**
@@ -35,9 +34,7 @@ export default function setNavigationVars(url, title, data, repl) {
35
34
  // Path does not
36
35
  bbn.env.path = substr(bbn.env.url, bbn.env.root.length);
37
36
  // Params will include each part of the URL
38
- bbn.env.params = filter(bbn.env.path.split('/'), function (v) {
39
- return v !== '';
40
- });
37
+ bbn.env.params = bbn.env.path.split('/').filter(function (v) { return v !== ''; });
41
38
  // Managing history
42
39
  var h = window.history;
43
40
  if (h) {
@@ -0,0 +1,49 @@
1
+ import { Filter } from './filterToConditions.js';
2
+ /**
3
+ * Returns a new array with only the data matching the given filter.
4
+ *
5
+ * The filtering arguments follow the same scheme as bbn.fn.search.
6
+ *
7
+ * @method _filter
8
+ * @global
9
+ * @example
10
+ * ```javascript
11
+ * let ar = [
12
+ * {name: "Raiders of the lost ark", director: "Steven Spielberg", year: 1981, id: 589},
13
+ * {name: "Goonies", director: "Richard Donner", year: 1985, id: 689},
14
+ * {name: "Star wars", director: "George Lucas", year: 1977, id: 256},
15
+ * {name: "Jaws", director: "Steven Spielberg", year: 1975, id: 423}
16
+ * ];
17
+ * bbn.fn.filter(ar, {director: "Steven Spielberg"});
18
+ * // [
19
+ * // {name: "Raiders of the lost ark", director: "Steven Spielberg", year: 1981, id: 589},
20
+ * // {name: "Jaws", director: "Steven Spielberg", year: 1975, id: 423}
21
+ * // ]
22
+ * bbn.fn.filter(ar, "director", "Steven Spielberg");
23
+ * // Same result as the previous example
24
+ * bbn.fn.filter(ar, {
25
+ * logic: "OR",
26
+ * conditions: [
27
+ * {
28
+ * field: "director",
29
+ * value: "Richard Donner"
30
+ * }, {
31
+ * field: "director",
32
+ * value: "George Lucas"
33
+ * }
34
+ * ]
35
+ * );
36
+ * // [
37
+ * // {name: "Goonies", director: "Richard Donner", year: 1985, id: 689},
38
+ * // {name: "Star wars", director: "George Lucas", year: 1977, id: 256},
39
+ * // ]
40
+ * ```
41
+ *
42
+ * @memberof bbn.fn
43
+ * @param {Array} arr The subject array
44
+ * @param {(String|Object|Function)} prop A property's name or a filter object or function
45
+ * @param {*} val The value with which comparing the given property
46
+ * @param {String} operator The operator to use for comparison with the value as used in bbn.fn.compare
47
+ * @returns {Array} A new filtered array
48
+ */
49
+ export default function _filter(arr: any[], prop: Filter | object | string | ((a: any, i: string | number | symbol) => boolean), val?: any, operator?: string, limit?: number): any[];
@@ -0,0 +1,100 @@
1
+ import isArray from '../type/isArray.js';
2
+ import compareConditions from './compareConditions.js';
3
+ /**
4
+ * Returns a new array with only the data matching the given filter.
5
+ *
6
+ * The filtering arguments follow the same scheme as bbn.fn.search.
7
+ *
8
+ * @method _filter
9
+ * @global
10
+ * @example
11
+ * ```javascript
12
+ * let ar = [
13
+ * {name: "Raiders of the lost ark", director: "Steven Spielberg", year: 1981, id: 589},
14
+ * {name: "Goonies", director: "Richard Donner", year: 1985, id: 689},
15
+ * {name: "Star wars", director: "George Lucas", year: 1977, id: 256},
16
+ * {name: "Jaws", director: "Steven Spielberg", year: 1975, id: 423}
17
+ * ];
18
+ * bbn.fn.filter(ar, {director: "Steven Spielberg"});
19
+ * // [
20
+ * // {name: "Raiders of the lost ark", director: "Steven Spielberg", year: 1981, id: 589},
21
+ * // {name: "Jaws", director: "Steven Spielberg", year: 1975, id: 423}
22
+ * // ]
23
+ * bbn.fn.filter(ar, "director", "Steven Spielberg");
24
+ * // Same result as the previous example
25
+ * bbn.fn.filter(ar, {
26
+ * logic: "OR",
27
+ * conditions: [
28
+ * {
29
+ * field: "director",
30
+ * value: "Richard Donner"
31
+ * }, {
32
+ * field: "director",
33
+ * value: "George Lucas"
34
+ * }
35
+ * ]
36
+ * );
37
+ * // [
38
+ * // {name: "Goonies", director: "Richard Donner", year: 1985, id: 689},
39
+ * // {name: "Star wars", director: "George Lucas", year: 1977, id: 256},
40
+ * // ]
41
+ * ```
42
+ *
43
+ * @memberof bbn.fn
44
+ * @param {Array} arr The subject array
45
+ * @param {(String|Object|Function)} prop A property's name or a filter object or function
46
+ * @param {*} val The value with which comparing the given property
47
+ * @param {String} operator The operator to use for comparison with the value as used in bbn.fn.compare
48
+ * @returns {Array} A new filtered array
49
+ */
50
+ export default function _filter(arr, prop, val, operator, limit) {
51
+ if (val === void 0) { val = null; }
52
+ if (operator === void 0) { operator = '='; }
53
+ if (limit === void 0) { limit = 0; }
54
+ if (!isArray(arr)) {
55
+ try {
56
+ arr = Array.from(arr);
57
+ }
58
+ catch (_a) {
59
+ bbn.fn.log("NOT ARRAY", arr);
60
+ throw new Error('Error in filter: The first argument must be an array');
61
+ }
62
+ }
63
+ var cfg = {};
64
+ var res = [];
65
+ var isFn = typeof (prop) === 'function';
66
+ if (!prop || !arr.length) {
67
+ return arr;
68
+ }
69
+ if (typeof prop === 'object') {
70
+ operator = val;
71
+ cfg = prop;
72
+ }
73
+ else if (typeof prop === 'string') {
74
+ cfg[prop] = val;
75
+ }
76
+ else if (!isFn) {
77
+ throw new Error('Search function error: The prop argument should be a string or an object');
78
+ }
79
+ var fn;
80
+ if (typeof (prop) === 'function') {
81
+ fn = prop;
82
+ }
83
+ else {
84
+ fn = function (a, i) { return compareConditions(a, cfg); };
85
+ }
86
+ for (var i = 0; i < arr.length; i++) {
87
+ bbn.env._enumerated.push(true);
88
+ if (fn(arr[i], i)) {
89
+ bbn.env._enumerated.pop();
90
+ res.push(arr[i]);
91
+ bbn.env._enumerated.push(true);
92
+ if (limit && (res.length >= limit)) {
93
+ break;
94
+ }
95
+ }
96
+ bbn.env._enumerated.pop();
97
+ }
98
+ return res;
99
+ }
100
+ ;
@@ -1,11 +1,5 @@
1
- interface Condition {
2
- field: string;
3
- operator?: string;
4
- value?: any;
5
- }
6
1
  interface Filter {
7
- conditions?: Condition[];
8
- logic?: string;
2
+ [key: string]: any;
9
3
  }
10
4
  /**
11
5
  * Converts the given object 'filter' to a valid format of condition.
@@ -1,5 +1,5 @@
1
1
  import checkType from '../type/checkType.js';
2
- import filter from './filter.js';
2
+ import _filter from './_filter.js';
3
3
  import each from '../loop/each.js';
4
4
  /**
5
5
  * Returns all the unique values of the given field (property) from the first object matching the given filter in an array.
@@ -32,7 +32,7 @@ import each from '../loop/each.js';
32
32
  export default function getFieldValues(arr, field, prop, val, operator) {
33
33
  checkType(field, 'string');
34
34
  if (prop) {
35
- arr = filter(arr, prop, val, operator);
35
+ arr = _filter(arr, prop, val, operator);
36
36
  }
37
37
  var res = [];
38
38
  each(arr, function (a) { return (res.indexOf(a[field]) === -1 ? res.push(a[field]) : null); });
@@ -114,9 +114,10 @@ export default function search(arr, prop, val, operator, startFrom, backward) {
114
114
  }
115
115
  var filter;
116
116
  var isFn = false;
117
+ var fn;
117
118
  if (!prop) {
118
119
  isFn = true;
119
- filter = function (a) {
120
+ fn = function (a) {
120
121
  return compareConditions({ value: a }, filterToConditions({
121
122
  logic: 'AND',
122
123
  conditions: [
@@ -145,7 +146,7 @@ export default function search(arr, prop, val, operator, startFrom, backward) {
145
146
  startFrom = typeof (val) === 'number' ? val : (backward ? arr.length - 1 : 0);
146
147
  if (typeof prop === 'function') {
147
148
  isFn = true;
148
- filter = prop;
149
+ fn = prop;
149
150
  }
150
151
  else if (isObject(prop)) {
151
152
  if ('field' in prop && 'value' in prop) {
@@ -184,37 +185,30 @@ export default function search(arr, prop, val, operator, startFrom, backward) {
184
185
  if (typeof startFrom !== 'number') {
185
186
  startFrom = backward ? arr.length - 1 : 0;
186
187
  }
187
- if (typeof filter === 'function') {
188
- if (backward) {
189
- for (var i = startFrom; i >= 0; i--) {
190
- if (filter(arr[i])) {
191
- res = i;
192
- }
193
- }
194
- }
195
- else {
196
- for (var i = startFrom; i < arr.length; i++) {
197
- if (filter(arr[i])) {
198
- res = i;
199
- }
188
+ if (!fn) {
189
+ var subfilter_1 = filterToConditions(filter);
190
+ fn = function (ele) { return compareConditions(ele, subfilter_1); };
191
+ }
192
+ if (backward) {
193
+ for (var i = startFrom; i >= 0; i--) {
194
+ bbn.env._enumerated.push(true);
195
+ if (fn(arr[i])) {
196
+ bbn.env._enumerated.pop();
197
+ arr[i];
198
+ return i;
200
199
  }
200
+ bbn.env._enumerated.pop();
201
201
  }
202
202
  }
203
203
  else {
204
- filter = filterToConditions(filter);
205
- if (backward) {
206
- for (var i = startFrom; i >= 0; i--) {
207
- if (compareConditions(arr[i], filter)) {
208
- res = i;
209
- }
210
- }
211
- }
212
- else {
213
- for (var i = startFrom; i < arr.length; i++) {
214
- if (compareConditions(arr[i], filter)) {
215
- res = i;
216
- }
204
+ for (var i = startFrom; i < arr.length; i++) {
205
+ bbn.env._enumerated.push(true);
206
+ if (fn(arr[i])) {
207
+ bbn.env._enumerated.pop();
208
+ arr[i];
209
+ return i;
217
210
  }
211
+ bbn.env._enumerated.pop();
218
212
  }
219
213
  }
220
214
  }
@@ -15,8 +15,6 @@
15
15
  * @returns {Array}
16
16
  */
17
17
  export default function unique(arr) {
18
- return arr.filter(function (el, index, ar) {
19
- return index === ar.indexOf(el);
20
- });
18
+ return arr.filter(function (el, index, ar) { return index === ar.indexOf(el); });
21
19
  }
22
20
  ;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bbn/bbn",
3
- "version": "1.0.433",
3
+ "version": "1.0.436",
4
4
  "description": "Javascript toolkit",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",