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

Allow For Previous Occurrences To Be Imported When Initiating Class #50

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@ export default class BananaSlug {
/**
* Create a new slug class.
*/
constructor () {
constructor (previousOccurrences) {
/** @type {Record<string, number>} */
// eslint-disable-next-line no-unused-expressions
this.occurrences

if(previousOccurrences){
this.occurrences = previousOccurrences.reduce((obj, slug) => {
const strippedSlug = slug.replace(/-\d+$/, '');
obj[strippedSlug] = (obj[strippedSlug] || 0) + 1;
return obj;
}, {});
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hey!

  • how do you want to save this stuff to a database?
  • the strippedSlug part comes with a bug: what if someone writes a heading ## Heading 1?
  • I think the this.reset below overwrites all your changes
  • docs and tests!
  • lastly, the previous points can be addressed first, but there’s also a security problem here: a slug of constructor will be injected into the object, and cause problems!

I’d first like to hear more about your use case.
Perhaps we can come up with a different API. Maybe like a slugger.toJSON() function which yields the map, and a Slugger.fromJSON(map) to revive it?

else {
this.occurrences
}
this.reset()
}

Expand Down
Loading