UNPKG

1.27 kBJavaScriptView Raw
1'use strict'
2
3var sorted = require('sorted-array-functions')
4var match = require('./match')
5var onlyRegex = require('./onlyRegex')
6var reverseSort = require('./reverseSort')
7
8function 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
15Bucket.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
30Bucket.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 // to remove all occurences
41 this.remove(pattern, payload)
42 break
43 }
44 }
45 }
46
47 return foundPattern
48}
49
50Bucket.prototype.forEach = function (func, that) {
51 this.data.forEach(func, that)
52 return this
53}
54
55module.exports = Bucket