New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for truthy/falsy boolean values #998
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
da5de23
Add support for truthy/falsy boolean values
WesTyler e7ee3e5
Simplify bool.truthy/falsy methods per feedback
WesTyler 2eaeb4f
Refactor boolean.truthy/falsy to be breaking changes
WesTyler fc457b7
Fix typo in API.md
WesTyler 566cdc1
Refactor boolean.truthy/falsy to utilize Set class
WesTyler 4c81267
Refactor Set methods to utilize Array methods for performance; simpli…
WesTyler 5649c87
Update Set class export style from exports.Set to module.exports
WesTyler 752cf85
Remove boolean _truthy/_falsy pattern
WesTyler File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
'use strict'; | ||
|
||
const Ref = require('./ref'); | ||
|
||
module.exports = class Set { | ||
|
||
constructor() { | ||
|
||
this._set = []; | ||
} | ||
|
||
add(value, refs) { | ||
|
||
if (!Ref.isRef(value) && this.has(value, null, null, false)) { | ||
|
||
return; | ||
} | ||
|
||
if (refs !== undefined) { // If it's a merge, we don't have any refs | ||
Ref.push(refs, value); | ||
} | ||
|
||
this._set.push(value); | ||
} | ||
|
||
merge(add, remove) { | ||
|
||
for (let i = 0; i < add._set.length; ++i) { | ||
this.add(add._set[i]); | ||
} | ||
|
||
for (let i = 0; i < remove._set.length; ++i) { | ||
this.remove(remove._set[i]); | ||
} | ||
} | ||
|
||
remove(value) { | ||
|
||
this._set = this._set.filter((item) => value !== item); | ||
} | ||
|
||
has(value, state, options, insensitive) { | ||
|
||
for (let i = 0; i < this._set.length; ++i) { | ||
let items = this._set[i]; | ||
|
||
if (state && Ref.isRef(items)) { // Only resolve references if there is a state, otherwise it's a merge | ||
items = items(state.reference || state.parent, options); | ||
} | ||
|
||
if (!Array.isArray(items)) { | ||
items = [items]; | ||
} | ||
|
||
for (let j = 0; j < items.length; ++j) { | ||
const item = items[j]; | ||
if (typeof value !== typeof item) { | ||
continue; | ||
} | ||
|
||
if (value === item || | ||
(value instanceof Date && item instanceof Date && value.getTime() === item.getTime()) || | ||
(insensitive && typeof value === 'string' && value.toLowerCase() === item.toLowerCase()) || | ||
(Buffer.isBuffer(value) && Buffer.isBuffer(item) && value.length === item.length && value.toString('binary') === item.toString('binary'))) { | ||
|
||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
values(options) { | ||
|
||
if (options && options.stripUndefined) { | ||
const values = []; | ||
|
||
for (let i = 0; i < this._set.length; ++i) { | ||
const item = this._set[i]; | ||
if (item !== undefined) { | ||
values.push(item); | ||
} | ||
} | ||
|
||
return values; | ||
} | ||
|
||
return this._set.slice(); | ||
} | ||
|
||
slice() { | ||
|
||
const newSet = new Set(); | ||
newSet._set = this._set.slice(); | ||
|
||
return newSet; | ||
} | ||
|
||
concat(source) { | ||
|
||
const newSet = new Set(); | ||
newSet._set = this._set.concat(source._set); | ||
|
||
return newSet; | ||
} | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
values() is already a copy, so I guess
description.truthyValues = this._inner._truthySet.values()
is enough.