Skip to content

Commit

Permalink
Implemented functionality
Browse files Browse the repository at this point in the history
Signed-off-by: Jaid <[email protected]>
  • Loading branch information
Jaid committed Feb 16, 2019
1 parent 005d29b commit 35df763
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 58 deletions.
8 changes: 6 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
"jest": {
"testEnvironment": "node"
},
"dependencies": {},
"dependencies": {
"lodash": "^4.17.11",
"lodash.isequal": "^4.5.0"
},
"devDependencies": {
"babel-jest": "^24.1.0",
"babel-preset-jaid": "^2.11.0",
Expand Down
58 changes: 24 additions & 34 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,37 @@
/** @module prevent-end */

/**
* @typedef valueGenerator
* @type {function}
* @param {string} value Original array entry
* @param {number} index Index of the array entry (starts at 0)
* @returns {*} Anything that will be the object entry value
*/
import {isEqual, takeRight, slice} from "lodash"

/**
* Converts an array to an object with static keys and customizable values
* Prevents a string or an array from having a specified end
* @example
* import preventEnd from "prevent-end"
* preventEnd(["a", "b"])
* // {a: null, b: null}
* import prevendEnd from "prevent-end"
* prevendEnd("abcd", "cd")
* // "ab"
* @example
* import preventEnd from "prevent-end"
* preventEnd(["a", "b"], "value")
* // {a: "value", b: "value"}
* @example
* import preventEnd from "prevent-end"
* preventEnd(["a", "b"], (key, index) => `value for ${key} #${index + 1}`)
* // {a: "value for a #1", b: "value for b #2"}
* import prevendEnd from "prevent-end"
* prevendEnd(["ab", "c" "d"], ["c", "d"])
* // ["ab"]
* @function
* @param {string[]} array Keys for the generated object
* @param {valueGenerator|*} [valueGenerator=null] Optional function that sets the object values based on key and index
* @returns {object<string, *>} A generated object based on the array input
* @param {string|array} value String or array that should not end with specified value
* @param {string|array} badEnd The unwanted end value
* @returns {*} A generated object based on the array input
*/
export default (array, valueGenerator = null) => {
if (!Array.isArray(array)) {
return {}
export default (value, badEnd) => {
if (typeof value === "string") {
if (value.endsWith(badEnd)) {
return value.substr(0, value.length - badEnd.length)
}
return value
}
const object = {}
if (typeof valueGenerator === "function") {
let index = 0
for (const value of array) {
object[value] = valueGenerator(value, index)
index++
if (Array.isArray(value, badEnd)) {
if (!Array.isArray(badEnd)) {
badEnd = [badEnd]
}
} else {
for (const value of array) {
object[value] = valueGenerator
if (isEqual(takeRight(value, badEnd.length), badEnd)) {
return slice(value, 0, value.length - badEnd.length)
}
return value
}
return object
return value
}
27 changes: 6 additions & 21 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,12 @@ import path from "path"
const indexModule = (process.env.MAIN ? path.resolve(__dirname, "..", process.env.MAIN) : path.join(__dirname, "..", "src"))
const {default: preventEnd} = require(indexModule)

it("should run with 1 argument", () => {
const result = preventEnd(["a", "b"])
expect(result).toEqual({
a: null,
b: null,
})
it("should run run for string", () => {
const result = preventEnd("abcd", "cd")
expect(result).toEqual("ab")
})

it("should run with an integer as second argument", () => {
const result = preventEnd(["a", "b"], 7)
expect(result).toEqual({
a: 7,
b: 7,
})
})

it("should run with a function as second argument", () => {
const valueGenerator = (key, index) => `${index + 1}-${key}-x`
const result = preventEnd(["a", "b"], valueGenerator)
expect(result).toEqual({
a: "1-a-x",
b: "2-b-x",
})
it("should run run for arrays", () => {
const result = preventEnd(["a", "b", "c", "d"], ["c", "d"])
expect(result).toEqual(["a", "b"])
})

0 comments on commit 35df763

Please sign in to comment.