From f99ee5e59ae8672453862e2ffad9d6823c9c8268 Mon Sep 17 00:00:00 2001 From: Sam Poder Date: Sat, 30 Sep 2023 00:24:57 -0700 Subject: [PATCH 1/2] Allow For Previous Occurrences To Be Imported When Initiating Class --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index dc8c3f1..9f9391e 100644 --- a/index.js +++ b/index.js @@ -9,10 +9,10 @@ export default class BananaSlug { /** * Create a new slug class. */ - constructor () { + constructor (previousOccurrences) { /** @type {Record} */ // eslint-disable-next-line no-unused-expressions - this.occurrences + this.occurrences = previousOccurrences ? previousOccurrences.reduce((obj, key) => ({ ...obj, [key]: 1 }), {}) : Object.create(null) this.reset() } From 9ce785567704a1455df1fa029ea74dfe40311e63 Mon Sep 17 00:00:00 2001 From: Sam Poder Date: Sat, 30 Sep 2023 00:31:42 -0700 Subject: [PATCH 2/2] Handle "-1", "-2" etc. --- index.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 9f9391e..0f2a118 100644 --- a/index.js +++ b/index.js @@ -12,8 +12,16 @@ export default class BananaSlug { constructor (previousOccurrences) { /** @type {Record} */ // eslint-disable-next-line no-unused-expressions - this.occurrences = previousOccurrences ? previousOccurrences.reduce((obj, key) => ({ ...obj, [key]: 1 }), {}) : Object.create(null) - + if(previousOccurrences){ + this.occurrences = previousOccurrences.reduce((obj, slug) => { + const strippedSlug = slug.replace(/-\d+$/, ''); + obj[strippedSlug] = (obj[strippedSlug] || 0) + 1; + return obj; + }, {}); + } + else { + this.occurrences + } this.reset() }