-
Notifications
You must be signed in to change notification settings - Fork 25
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 JS Set #60
Open
jayvdb
wants to merge
3
commits into
flexxui:master
Choose a base branch
from
jayvdb:js-set
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add JS Set #60
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -141,6 +141,14 @@ def get_all_std_names(): | |
} | ||
}""" | ||
|
||
FUNCTIONS['create_set'] = """function () { | ||
var s = new Set(); | ||
for (var i=0; i<arguments.length; i+=2) { | ||
if (!s.has(arguments[i]) && !FUNCTION_PREFIXop_contains(arguments[i], s)) { s.add(arguments[i]) }; | ||
almarklein marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
return s; | ||
}""" | ||
|
||
FUNCTIONS['create_dict'] = """function () { | ||
var d = {}; | ||
for (var i=0; i<arguments.length; i+=2) { d[arguments[i]] = arguments[i+1]; } | ||
|
@@ -224,6 +232,16 @@ def get_all_std_names(): | |
return r; | ||
}""" | ||
|
||
FUNCTIONS['set'] = """function (x) { // nargs: 0 1 | ||
var s = new Set(); | ||
if (!x) { return s; }; | ||
if (typeof x==="object" && !Array.isArray(x)) {x = Object.keys(x)} | ||
for (var i=0; i<x.length; i++) { | ||
if (!s.has(x[i]) && !FUNCTION_PREFIXop_contains(x[i], s)) { s.add(x[i]) }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can do just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, right, nested data ... |
||
} | ||
return s; | ||
}""" | ||
|
||
FUNCTIONS['range'] = """function (start, end, step) { | ||
var i, res = []; | ||
var val = start; | ||
|
@@ -398,6 +416,7 @@ def get_all_std_names(): | |
FUNCTIONS['truthy'] = """function (v) { | ||
if (v === null || typeof v !== "object") {return v;} | ||
else if (v.length !== undefined) {return v.length ? v : false;} | ||
else if (v.size !== undefined) {return v.size ? v : false;} | ||
else if (v.byteLength !== undefined) {return v.byteLength ? v : false;} | ||
else if (v.constructor !== Object) {return true;} | ||
else {return Object.getOwnPropertyNames(v).length ? v : false;} | ||
|
@@ -411,6 +430,10 @@ def get_all_std_names(): | |
return a == b; | ||
} | ||
|
||
if (a && a.constructor === Set && b && b.constructor === Set) { | ||
a = Array.from(a); | ||
b = Array.from(b); | ||
almarklein marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
if (a == null || b == null) { | ||
} else if (Array.isArray(a) && Array.isArray(b)) { | ||
var i = 0, iseq = a.length == b.length; | ||
|
@@ -427,6 +450,10 @@ def get_all_std_names(): | |
}""" | ||
|
||
FUNCTIONS['op_contains'] = """function op_contains (a, b) { // nargs: 2 | ||
if (b && b.constructor === Set) { | ||
if (b.has(a)) return true; | ||
b = Array.from(b); | ||
} | ||
if (b == null) { | ||
} else if (Array.isArray(b)) { | ||
for (var i=0; i<b.length; i++) {if (FUNCTION_PREFIXop_equals(a, b[i])) | ||
|
@@ -458,6 +485,45 @@ def get_all_std_names(): | |
} return a * b; | ||
}""" | ||
|
||
FUNCTIONS['op_set_union'] = """function (a, b) { // nargs: 2 | ||
let _union = new Set(a) | ||
for (let elem of b) { | ||
if (!_union.has(elem) && !FUNCTION_PREFIXop_contains(elem, _union)) { _union.add(elem) }; | ||
almarklein marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
return _union; | ||
}""" | ||
|
||
FUNCTIONS['op_set_intersection'] = """function (a, b) { // nargs: 2 | ||
let _intersection = new Set(); | ||
for (let elem of b) { | ||
if (a.has(elem) && FUNCTION_PREFIXop_contains(elem, a)) { | ||
_intersection.add(elem); | ||
} | ||
} | ||
return _intersection; | ||
}""" | ||
|
||
FUNCTIONS['op_set_sym_difference'] = """function (a, b) { // nargs: 2 | ||
let _difference = new Set(a) | ||
for (let elem of b) { | ||
if (_difference.has(elem) && FUNCTION_PREFIXop_contains(elem, _difference)) { | ||
_difference.delete(elem); | ||
} else { | ||
_difference.add(elem); | ||
} | ||
} | ||
return _difference; | ||
}""" | ||
|
||
FUNCTIONS['op_set_difference'] = """function (a, b) { // nargs: 2 | ||
let _difference = new Set(a) | ||
for (let elem of b) { | ||
if (_difference.has(elem) && FUNCTION_PREFIXop_contains(elem, _difference)) { | ||
_difference.delete(elem); | ||
} | ||
} | ||
return _difference; | ||
}""" | ||
|
||
## ----- Methods | ||
|
||
|
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
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.
I'm a bit unsure about this. It allows set operations on literal sets only. The use is rather limited because in most cases you'd just directly write the result. But more importantly, it may fool users into thinking that operators work for sets, which is not the case (except for literals).
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.
I'm happy to drop these for the moment. They are not in my actual use case. I just included them to outline how they could be implemented on top of the JS Set.