Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | 1x 1x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 103x 103x 103x 22x 22x 22x 2x 2x 2x 2x 2x 2x 2x 22x 22x 22x 105x 19x 19x 22x 22x 22x 105x 19x 19x 22x 22x 1x 1x 105x 105x 105x 105x 105x 105x 105x 19x 19x 105x 19x 19x 105x 98x 98x 105x 105x 105x 105x 105x 105x 105x 105x 7x 2x 2x 7x 105x 98x 98x 105x 105x 105x 1x 1x 105x 105x 1x 1x 104x 105x 1x 1x 103x 103x 103x 103x 103x 105x 19x 19x 19x 19x 19x 19x 19x 84x 84x 84x 84x 84x 1x 1x 7x 1x 1x 1x 7x 6x 6x 7x 1x | export class Unique {
constructor(pure) {
this.pure = pure;
/**
* global results store
* currently uniqueness is global to entire pure instance
* this means that pure should currently *never* return
* duplicate values across all API methods when using `pure.unique`
* it's possible in the future that some users may want to scope
* found per function call instead of pure instance
*/
this.globalFound = {};
/**
* scoped results store
*/
this.scopedFound = {};
/**
* global exclude list of results
* defaults to nothing excluded
*/
this.exclude = [];
this.startTime = 0;
this.currentIterations = 0;
/**
* uniqueness compare function
* default behavior is to check value as key against object hash
*/
this.defaultCompare = (obj, key) => {
if (typeof obj[key] === 'undefined') {
return -1;
}
return 0;
};
this.errorMessage = (now, code) => {
throw new Error(`
Started time: ${now}
${code} for uniqueness check.
May not be able to generate any more unique values with current settings.
Try adjusting maxTime or maxRetries parameters for pure.unique()`);
};
this.setStartTime = () => {
if (this.startTime === 0) {
this.startTime = new Date().getTime();
}
};
this.setCurrentIterations = () => {
if (!this.currentIterations > 0) {
this.currentIterations = 0;
}
};
}
exec(method, args, opts = {}) {
const options = opts;
const now = new Date().getTime();
this.setStartTime();
this.setCurrentIterations();
if (typeof options.maxTime !== 'number') {
options.maxTime = 50;
}
if (typeof options.maxRetries !== 'number') {
options.maxRetries = 500;
}
if (typeof options.scope !== 'string') {
options.scope = undefined;
}
options.exclude = options.exclude || this.exclude;
options.compare = options.compare || this.defaultCompare;
let found = {};
// scope definition
if (options.scope) {
if (!this.scopedFound[options.scope]) {
this.scopedFound[options.scope] = {};
}
found = this.scopedFound[options.scope];
} else {
found = this.globalFound;
}
// support single exclude argument as string
if (typeof options.exclude === 'string') {
options.exclude = [options.exclude];
}
if (now - this.startTime >= options.maxTime) {
return this.errorMessage(now, `Exceeded maxTime: ${options.maxTime}`);
}
if (this.currentIterations >= options.maxRetries) {
return this.errorMessage(now, `Exceeded maxRetries: ${options.maxRetries}`);
}
// execute the provided method to find a potential satifised value
const result = method.apply({ pure: this.pure }, args);
// if the result has not been previously found, add it to the found array and return the value as it's unique
if (options.compare(found, result) === -1 && options.exclude.indexOf(result) === -1) {
found[result] = result;
this.exclude = [];
this.startTime = 0;
this.currentIterations = 0;
return result;
}
this.currentIterations += 1;
return this.exec(method, args, options);
}
clear(scope) {
if (scope) {
if (this.scopedFound[scope]) {
this.scopedFound[scope] = undefined;
}
} else {
this.globalFound = {};
}
}
}
|