@factor/filters
Advanced tools
Comparing version 1.0.0-beta.4 to 1.0.0-beta.6
@@ -6,4 +6,12 @@ # Change Log | ||
# [1.0.0-beta.4](https://github.com/fiction-com/factor/compare/v1.0.0-beta.3...v1.0.0-beta.4) (2019-09-30) | ||
# [1.0.0-beta.6](https://github.com/fiction-com/factor/compare/v1.0.0-beta.5...v1.0.0-beta.6) (2019-10-25) | ||
**Note:** Version bump only for package @factor/filters | ||
# [1.0.0-beta.5](https://github.com/fiction-com/factor/compare/v1.0.0-beta.4...v1.0.0-beta.5) (2019-10-25) | ||
**Note:** Version bump only for package @factor/filters |
125
index.js
@@ -1,3 +0,7 @@ | ||
module.exports.default = Factor => { | ||
return new (class { | ||
import Factor from "@factor/core" | ||
import { sortPriority, uniqueObjectHash } from "@factor/tools/utils" | ||
// Singleton | ||
if (!Factor.$filters) { | ||
class FactorFilters { | ||
constructor() { | ||
@@ -8,46 +12,2 @@ this._filters = {} | ||
_sort(arr) { | ||
return arr.sort((a, b) => { | ||
const ap = a.priority || 100 | ||
const bp = b.priority || 100 | ||
if (ap < bp) { | ||
return -1 | ||
} else if (ap > bp) { | ||
return 1 | ||
} else { | ||
return 0 | ||
} | ||
}) | ||
} | ||
uniqueHash(obj, salt = "") { | ||
if (!obj) { | ||
return obj | ||
} | ||
let str | ||
if (typeof obj == "string") { | ||
str = obj | ||
} else if (typeof obj == "function") { | ||
str = obj.toString() | ||
} else { | ||
// Make sure to remove circular refs | ||
// https://github.com/WebReflection/flatted#flatted | ||
const { stringify } = require("flatted/cjs") | ||
str = stringify(obj) | ||
} | ||
str = str + salt | ||
str = str.substring(0, 500) | ||
return str | ||
.split("") | ||
.reduce( | ||
(prevHash, currVal) => ((prevHash << 5) - prevHash + currVal.charCodeAt(0)) | 0, | ||
0 | ||
) | ||
} | ||
// Get total number of filters added on an id | ||
@@ -74,7 +34,6 @@ count(name) { | ||
const _addedArray = Object.keys(_added).map(i => _added[i]) | ||
const _sorted = this._sort(_addedArray) | ||
const _sorted = sortPriority(_addedArray) | ||
for (let i = 0; i < _sorted.length; i++) { | ||
const { callback, context } = _sorted[i] | ||
for (const element of _sorted) { | ||
const { callback, context } = element | ||
const result = callback.apply(context, params) | ||
@@ -93,3 +52,3 @@ | ||
if (Array.isArray(data)) { | ||
data = this._sort(data) | ||
data = sortPriority(data) | ||
} | ||
@@ -103,5 +62,3 @@ | ||
add(id, filter, { context = false, priority = 100, key = "", reloads = false } = {}) { | ||
if (!this._filters[id]) { | ||
this._filters[id] = {} | ||
} | ||
if (!this._filters[id]) this._filters[id] = {} | ||
@@ -111,11 +68,4 @@ // create unique ID | ||
// Using objects and a hash identifier solves that | ||
const filterKey = `key_${this.uniqueHash(filter, key)}` | ||
const filterKey = `key_${uniqueObjectHash(filter, this.callerKey(key))}` | ||
if (this._filters[id][filterKey] && !reloads) { | ||
Factor.$log.warn( | ||
`Duplicate filter signature detected adding to "${id}" filter.\nSet "key" option to a unique value or set "reloads" true to silence this warning.` | ||
) | ||
//console.log(filter.toString()) | ||
} | ||
// For simpler assignments where no callback is needed | ||
@@ -131,15 +81,11 @@ const callback = typeof filter != "function" ? () => filter : filter | ||
push(id, item, options = {}) { | ||
push(_id, item, options = {}) { | ||
const { key = "" } = options | ||
options.key = this.uniqueHash(item, key) | ||
options.key = uniqueObjectHash(item, this.callerKey(key)) | ||
this.add( | ||
id, | ||
_id, | ||
(_, args) => { | ||
item = typeof item == "function" ? item(args) : item | ||
if (Array.isArray(_)) { | ||
return [..._, item] | ||
} else if (typeof _ == "object") { | ||
return { ..._, [this.uniqueHash(item)]: item } | ||
} | ||
return [..._, item] | ||
}, | ||
@@ -150,2 +96,16 @@ options | ||
register(_id, _property, item, options = {}) { | ||
const { key = "" } = options | ||
options.key = uniqueObjectHash(item, this.callerKey(key)) | ||
this.add( | ||
_id, | ||
(_, args) => { | ||
item = typeof item == "function" ? item(args) : item | ||
return { ..._, [_property]: item } | ||
}, | ||
options | ||
) | ||
} | ||
// Add callbacks into an array of promises, meant to be used with $filters.run | ||
@@ -156,3 +116,3 @@ callback(id, callback, options = {}) { | ||
const { key = "" } = options | ||
options.key = this.uniqueHash(callback, key) | ||
options.key = uniqueObjectHash(callback, this.callerKey(key)) | ||
@@ -165,6 +125,23 @@ const callable = typeof callback != "function" ? () => callback : callback | ||
// Run array of promises and await the result | ||
async run(id, args = {}) { | ||
return await Promise.all(this.apply(id, [], args)) | ||
async run(id, _arguments = {}) { | ||
return await Promise.all(this.apply(id, [], _arguments)) | ||
} | ||
})() | ||
// Use the function that called the filter in the key | ||
// this prevents issues where two filters in different may match each other | ||
// which causes difficult to solve bugs (data-schemas is an example) | ||
callerKey(key) { | ||
return ( | ||
key + | ||
new Error().stack | ||
.toString() | ||
.split("at") | ||
.find(line => !line.match(/(filter|Error)/)) | ||
) | ||
} | ||
} | ||
Factor.$filters = Factor.prototype.$filters = new FactorFilters() | ||
} | ||
export default Factor.$filters |
{ | ||
"name": "@factor/filters", | ||
"version": "1.0.0-beta.4", | ||
"version": "1.0.0-beta.6", | ||
"license": "GPL-2.0", | ||
@@ -9,6 +9,3 @@ "publishConfig": { | ||
"factor": {}, | ||
"gitHead": "9e9aa5d0228d01db9af1c17feddbe5fecab83700", | ||
"dependencies": { | ||
"flatted": "^2.0.1" | ||
} | ||
"gitHead": "69876258f0f616715ac7ecf17cddea2c1d59de36" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24112
0
5
126
- Removedflatted@^2.0.1
- Removedflatted@2.0.2(transitive)