1 | 'use strict'
|
2 |
|
3 | var sorted = require('sorted-array-functions')
|
4 | var match = require('./match')
|
5 | var onlyRegex = require('./onlyRegex')
|
6 | var reverseSort = require('./reverseSort')
|
7 |
|
8 | function Bucket (parent) {
|
9 | this.data = []
|
10 | this._algo = parent._algo
|
11 | this._isDeep = parent._isDeep
|
12 | this.magic = this._isDeep ? 0 : Number.MAX_SAFE_INTEGER
|
13 | }
|
14 |
|
15 | Bucket.prototype.add = function (set) {
|
16 | sorted.add(this.data, set, this.magic !== set.magic ? this._algo : reverseSort)
|
17 |
|
18 | if (this._isDeep) {
|
19 | if (this.magic < set.magic) {
|
20 | this.magic = set.magic
|
21 | }
|
22 | } else {
|
23 | if (this.magic > set.magic) {
|
24 | this.magic = set.magic
|
25 | }
|
26 | }
|
27 | return this
|
28 | }
|
29 |
|
30 | Bucket.prototype.remove = function (pattern, payload) {
|
31 | var foundPattern = false
|
32 | var data = this.data
|
33 | var justRegex = onlyRegex(pattern)
|
34 | for (var i = 0; i < data.length; i++) {
|
35 | if (match(pattern, data[i].pattern)) {
|
36 | if (payload === null || payload === data[i].payload || justRegex) {
|
37 | data.splice(i, 1)
|
38 | foundPattern = true
|
39 |
|
40 |
|
41 | this.remove(pattern, payload)
|
42 | break
|
43 | }
|
44 | }
|
45 | }
|
46 |
|
47 | return foundPattern
|
48 | }
|
49 |
|
50 | Bucket.prototype.forEach = function (func, that) {
|
51 | this.data.forEach(func, that)
|
52 | return this
|
53 | }
|
54 |
|
55 | module.exports = Bucket
|