Skip to content

Commit

Permalink
Implemented functionality
Browse files Browse the repository at this point in the history
Signed-off-by: Jaid <jaid.jsx@gmail.com>
  • Loading branch information
Jaid committed Feb 16, 2019
1 parent cb9d394 commit 38d0a83
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
40 changes: 20 additions & 20 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
/** @module ensure-start */

import {isEqual, takeRight, slice} from "lodash"
import {isEqual, take} from "lodash"

/**
* Prevents a string or an array from having a specified end
* Ensures that a string or an array starts with specified value
* @example
* import prevendEnd from "ensure-start"
* ensureStart("abcd", "cd")
* // "ab"
* import ensureStart from "ensure-start"
* ensureStart("cd", "ab")
* // "abcd"
* @example
* import prevendEnd from "ensure-start"
* ensureStart(["ab", "c" "d"], ["c", "d"])
* // ["ab"]
* import ensureStart from "ensure-start"
* ensureStart(["ab"], ["c", "d"])
* // ["c", "d", "ab"]
* @function
* @param {string|array} value String or array that should not end with specified value
* @param {string|array} badEnd The unwanted end value
* @param {string|array} value String or array that should start with specified value
* @param {string|array} ensuredStart The wanted start value
* @returns {*} Cleaned value
*/
export default (value, badEnd) => {
export default (value, ensuredStart) => {
if (typeof value === "string") {
if (value.endsWith(badEnd)) {
return value.substr(0, value.length - badEnd.length)
if (value.startsWith(ensuredStart)) {
return value
}
return value
return `${ensuredStart}${value}`
}
if (Array.isArray(value, badEnd)) {
if (!Array.isArray(badEnd)) {
badEnd = [badEnd]
if (Array.isArray(value, ensuredStart)) {
if (!Array.isArray(ensuredStart)) {
ensuredStart = [ensuredStart]
}
if (isEqual(takeRight(value, badEnd.length), badEnd)) {
return slice(value, 0, value.length - badEnd.length)
if (isEqual(take(value, ensuredStart.length), ensuredStart)) {
return value
}
return value
return [...ensuredStart, ...value]
}
return value
}
12 changes: 6 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import path from "path"
const indexModule = (process.env.MAIN ? path.resolve(__dirname, "..", process.env.MAIN) : path.join(__dirname, "..", "src"))
const {default: ensureStart} = require(indexModule)

it("should run run for string", () => {
const result = ensureStart("abcd", "cd")
expect(result).toEqual("ab")
it("should run for string", () => {
const result = ensureStart("cd", "ab")
expect(result).toEqual("abcd")
})

it("should run run for arrays", () => {
const result = ensureStart(["a", "b", "c", "d"], ["c", "d"])
expect(result).toEqual(["a", "b"])
it("should run for arrays", () => {
const result = ensureStart(["c", "d"], ["a", "b"])
expect(result).toEqual(["a", "b", "c", "d"])
})

0 comments on commit 38d0a83

Please sign in to comment.