Skip to content
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 left and right joins #23

Merged
merged 8 commits into from
Jul 13, 2017

Conversation

jstcki
Copy link
Contributor

@jstcki jstcki commented Jul 12, 2017

Fixes #21.

I also added a new test file. The results are the following:

# Left
./ndjson-join 'd.name' <(csv2json -n test/a.csv) <(csv2json -n test/c.csv) --left
[{"name":"Fred","color":"red"},{"name":"Fred","number":"42"}]
[{"name":"Alice","color":"green"},null]
[{"name":"Bob","color":"blue"},null]

# Right
./ndjson-join 'd.name' <(csv2json -n test/c.csv) <(csv2json -n test/a.csv) --right
[{"name":"Fred","number":"42"},{"name":"Fred","color":"red"}]
[null,{"name":"Alice","color":"green"}]
[null,{"name":"Bob","color":"blue"}]

# Inner (default)
./ndjson-join 'd.name' <(csv2json -n test/a.csv) <(csv2json -n test/c.csv)
[{"name":"Fred","color":"red"},{"name":"Fred","number":"42"}]

ndjson-join Outdated
@@ -13,6 +13,8 @@ commander
.usage("[options] [expression₀ [expression₁]] <file₀> <file₁>")
.description("Join two newline-delimited JSON streams into a stream of pairs.")
.option("-r, --require <name=module>", "require a module", requires, {d: undefined, i: -1})
.option("--left", "Perform a left join", false)
.option("--right", "Perform a right join", false)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lowercase “perform”.

ndjson-join Outdated
@@ -25,6 +27,8 @@ if (commander.args.length < 4) {
commander.args.splice(1, 0, commander.args[0]);
}

var join = commander.left ? leftJoin : commander.right ? rightJoin : innerJoin;

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if both --left and --right are set, --left takes priority? Perhaps instead, if both are set we should do an outerJoin?

ndjson-join Outdated
map.forEach(function(value, key) {
value[0].forEach(function(v0) {
if (value[1].length === 0) {
output([v0, null])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing semicolon.

ndjson-join Outdated
function leftJoin() {
map.forEach(function(value, key) {
value[0].forEach(function(v0) {
if (value[1].length === 0) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’d invert the logic like so:

if (value[1].length) {
  
} else {
  output([v0, null]);
}

ndjson-join Outdated
map.forEach(function(value, key) {
value[1].forEach(function(v1) {
if (value[0].length === 0) {
output([null, v1])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Semicolon.

ndjson-join Outdated
function rightJoin() {
map.forEach(function(value, key) {
value[1].forEach(function(v1) {
if (value[0].length === 0) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Invert logic as above.

@mbostock
Copy link
Owner

Good stuff! Biggest suggestion would be to support outer join either as --left --right or as --outer. (If the latter, then the arguments should be exclusive and specifying multiple types of join should exit with an error.)

Can you update the README to document the new arguments?

@jstcki
Copy link
Contributor Author

jstcki commented Jul 13, 2017

Done! Thanks for the feedback.

One thing I wondered: wouldn't it be better to output an empty object instead of null for missing values? That might be easier to handle later in the chain because for example Object.assign(null, {foo: 1}) will fail.

@mbostock mbostock merged commit a0b62dd into mbostock:master Jul 13, 2017
@mbostock
Copy link
Owner

Released in 0.3.1! Thank you!

@jstcki jstcki deleted the left-right-join branch July 17, 2017 09:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants