-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat: 🎸 add [dedupe] #385
Merged
feat: 🎸 add [dedupe] #385
Changes from all commits
Commits
Show all changes
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import _ from 'lodash'; | ||
/** | ||
* Take `Object`, and check that the object identifier has not already been used previously | ||
* | ||
* @param {String} [path = uri] path containing the object Identifier | ||
* @param {Boolean} [ignore = false] Just ignore duplicate object | ||
* @returns {Object} | ||
*/ | ||
export default async function identify(data, feed) { | ||
const pathName = this.getParam('path', 'uri'); | ||
const ignore = Boolean(this.getParam('ignore', false)); | ||
const path = Array.isArray(pathName) ? pathName.shift() : pathName; | ||
const check = 'no uri!'; | ||
const uri = _.get(data, path, check); | ||
if (!this.previousURI) { | ||
this.previousURI = {}; | ||
} | ||
if (this.isLast()) { | ||
return feed.close(); | ||
} | ||
if (uri === check) { | ||
if (ignore) { | ||
console.warn(`WARNING: ${path} field not exists, item #${this.getIndex()} was ignored` ); | ||
return feed.end(); | ||
} | ||
return feed.send(new Error(`${path} field not exists, enable to dedupe.`)); | ||
} | ||
if (this.previousURI[uri] === true) { | ||
if (ignore) { | ||
console.warn(`WARNING: ${uri} already exists, item #${this.getIndex()} was ignored` ); | ||
return feed.end(); | ||
} | ||
return feed.send(new Error(`Duplicate identifier: ${uri} already exists`)); | ||
} | ||
this.previousURI[uri] = true; | ||
return feed.send(data); | ||
} |
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,142 @@ | ||
import assert from 'assert'; | ||
import from from 'from'; | ||
import ezs from '../src'; | ||
import statements from '../src/statements'; | ||
|
||
ezs.use(statements); | ||
|
||
describe('[dedupe]', () => { | ||
it('with error (1)', (done) => { | ||
const input = [ | ||
{ a: 1, b: 'a' }, | ||
{ a: 1, b: 'b' }, | ||
{ a: 3, b: 'c' }, | ||
{ a: 4, b: 'd' }, | ||
{ a: 5, b: 'e' }, | ||
{ a: 6, b: 'f' }, | ||
]; | ||
const output = []; | ||
from(input) | ||
.pipe(ezs('dedupe', { path: ['a', 'c'] })) | ||
.on('data', (chunk) => { | ||
output.push(chunk); | ||
}) | ||
.on('end', () => { | ||
assert.equal(output.length, 6); | ||
assert.ok(output[1] instanceof Error); | ||
assert.ok(output[1].message.includes('Duplicate identifier: 1 already exists')); | ||
done(); | ||
}); | ||
}); | ||
it('with error (2)', (done) => { | ||
const input = [ | ||
{ a: 1, b: 'a' }, | ||
{ a: 1, b: 'b' }, | ||
{ a: 3, b: 'c' }, | ||
{ a: 3, b: 'd' }, | ||
{ a: 4, b: 'e' }, | ||
{ a: 4, b: 'f' }, | ||
]; | ||
const output = []; | ||
from(input) | ||
.pipe(ezs('dedupe', { path: 'a' })) | ||
.on('data', (chunk) => { | ||
output.push(chunk); | ||
}) | ||
.on('end', () => { | ||
assert.equal(output.length, 6); | ||
assert.ok(output[1] instanceof Error); | ||
assert.ok(output[3] instanceof Error); | ||
assert.ok(output[5] instanceof Error); | ||
done(); | ||
}); | ||
}); | ||
it('with ignore (1)', (done) => { | ||
const input = [ | ||
{ a: 1, b: 'a' }, | ||
{ a: 1, b: 'b' }, | ||
{ a: 3, b: 'c' }, | ||
{ a: 4, b: 'd' }, | ||
{ a: 5, b: 'e' }, | ||
{ a: 6, b: 'f' }, | ||
]; | ||
const output = []; | ||
from(input) | ||
.pipe(ezs('dedupe', { path: ['a', 'c'], ignore: true })) | ||
.on('data', (chunk) => { | ||
output.push(chunk); | ||
}) | ||
.on('end', () => { | ||
assert.equal(output.length, 5); | ||
done(); | ||
}); | ||
}); | ||
it('with ignore (2)', (done) => { | ||
const input = [ | ||
{ a: 1, b: 'a' }, | ||
{ a: 1, b: 'b' }, | ||
{ a: 3, b: 'c' }, | ||
{ a: 3, b: 'd' }, | ||
{ a: 4, b: 'e' }, | ||
{ a: 4, b: 'f' }, | ||
]; | ||
const output = []; | ||
from(input) | ||
.pipe(ezs('dedupe', { path: 'a', ignore: true })) | ||
.on('data', (chunk) => { | ||
output.push(chunk); | ||
}) | ||
.on('end', () => { | ||
assert.equal(output.length, 3); | ||
done(); | ||
}); | ||
}); | ||
it('with no uri', (done) => { | ||
const input = [ | ||
{ a: 1, b: 'a' }, | ||
{ a: 1, b: 'b' }, | ||
{ a: 3, b: 'c' }, | ||
{ a: 3, b: 'd' }, | ||
{ a: 4, b: 'e' }, | ||
{ a: 4, b: 'f' }, | ||
]; | ||
const output = []; | ||
from(input) | ||
.pipe(ezs('dedupe')) | ||
.on('data', (chunk) => { | ||
output.push(chunk); | ||
}) | ||
.on('end', () => { | ||
assert.equal(output.length, 6); | ||
assert.ok(output[0] instanceof Error); | ||
assert.ok(output[0].message.includes('uri field not exists, enable to dedupe.')); | ||
assert.ok(output[1] instanceof Error); | ||
assert.ok(output[2] instanceof Error); | ||
assert.ok(output[3] instanceof Error); | ||
assert.ok(output[4] instanceof Error); | ||
assert.ok(output[5] instanceof Error); | ||
done(); | ||
}); | ||
}); | ||
it('with no uri (ignore)', (done) => { | ||
const input = [ | ||
{ a: 1, b: 'a' }, | ||
{ a: 1, b: 'b' }, | ||
{ a: 3, b: 'c' }, | ||
{ a: 3, b: 'd' }, | ||
{ a: 4, b: 'e' }, | ||
{ a: 4, b: 'f' }, | ||
]; | ||
const output = []; | ||
from(input) | ||
.pipe(ezs('dedupe', { ignore: true })) | ||
.on('data', (chunk) => { | ||
output.push(chunk); | ||
}) | ||
.on('end', () => { | ||
assert.equal(output.length, 0); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
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.
Shouldn't that function be named
dedupe
, and notidentify
?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.
of course, it will be better for the doc ...
and I will correct another similar error