Skip to content

Commit

Permalink
Add trim function to string module (#171)
Browse files Browse the repository at this point in the history
* add trim function to string module
  • Loading branch information
sodiray authored Nov 25, 2022
1 parent 9d7ce47 commit e9ae1b2
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 5 deletions.
8 changes: 7 additions & 1 deletion cdn/radash.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,5 +783,11 @@ const template = (str, data, regex = /\{\{(.+?)\}\}/g) => {
return acc.replace(match[0], data[match[1]]);
}, str);
};
const trim = (str, charsToTrim = " ") => {
if (!str)
return "";
const regex = new RegExp(`^[${charsToTrim}]+|[${charsToTrim}]+$`, "g");
return str.replace(regex, "");
};

export { alphabetical, boil, callable, camel as camal, camel, capitalize, chain, clone, cluster, compose, counting, dash, debounce, defer, diff, draw, first, flat, fork, get, group, intersects, invert, isArray, isDate, isEmpty, isEqual, isFloat, isFunction, isInt, isNumber, isObject, isPrimitive, isString, isSymbol, iterate, last, list, listify, lowerize, map, mapEntries, mapKeys, mapValues, max, memo, merge, min, objectify, omit, parallel, partial, partob, pascal, pick, proxied, random, range, reduce, replace, replaceOrAppend, retry, select, series, shake, shift, shuffle, sift, sleep, snake, sort, sum, template, throttle, title, toFloat, toInt, toggle, tryit as try, tryit, uid, unique, upperize, zip };
export { alphabetical, boil, callable, camel as camal, camel, capitalize, chain, clone, cluster, compose, counting, dash, debounce, defer, diff, draw, first, flat, fork, get, group, intersects, invert, isArray, isDate, isEmpty, isEqual, isFloat, isFunction, isInt, isNumber, isObject, isPrimitive, isString, isSymbol, iterate, last, list, listify, lowerize, map, mapEntries, mapKeys, mapValues, max, memo, merge, min, objectify, omit, parallel, partial, partob, pascal, pick, proxied, random, range, reduce, replace, replaceOrAppend, retry, select, series, shake, shift, shuffle, sift, sleep, snake, sort, sum, template, throttle, title, toFloat, toInt, toggle, trim, tryit as try, tryit, uid, unique, upperize, zip };
7 changes: 7 additions & 0 deletions cdn/radash.js
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,12 @@ var radash = (function (exports) {
return acc.replace(match[0], data[match[1]]);
}, str);
};
const trim = (str, charsToTrim = " ") => {
if (!str)
return "";
const regex = new RegExp(`^[${charsToTrim}]+|[${charsToTrim}]+$`, "g");
return str.replace(regex, "");
};

exports.alphabetical = alphabetical;
exports.boil = boil;
Expand Down Expand Up @@ -865,6 +871,7 @@ var radash = (function (exports) {
exports.toFloat = toFloat;
exports.toInt = toInt;
exports.toggle = toggle;
exports.trim = trim;
exports.try = tryit;
exports.tryit = tryit;
exports.uid = uid;
Expand Down
2 changes: 1 addition & 1 deletion cdn/radash.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "radash",
"version": "9.4.2",
"version": "9.5.0",
"description": "Functional utility library - modern, simple, typed, powerful",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",
Expand Down Expand Up @@ -48,4 +48,4 @@
"engines": {
"node": ">=14.18.0"
}
}
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ export {
pascal,
snake,
template,
title
title,
trim
} from './string'
export {
isArray,
Expand Down
20 changes: 20 additions & 0 deletions src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,23 @@ export const template = (
return acc.replace(match[0], data[match[1]])
}, str)
}

/**
* Trims all prefix and suffix characters from the given
* string. Like the builtin trim function but accepts
* other characters you would like to trim.
*
* ```typescript
* trim(' hello ') // => 'hello'
* trim('__hello__', '_') // => 'hello'
* trim('/repos/:owner/:repo/', '/') // => 'repos/:owner/:repo'
* ```
*/
export const trim = (
str: string | null | undefined,
charsToTrim: string = ' '
) => {
if (!str) return ''
const regex = new RegExp(`^[${charsToTrim}]+|[${charsToTrim}]+$`, 'g')
return str.replace(regex, '')
}
15 changes: 15 additions & 0 deletions src/tests/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,19 @@ describe('string module', () => {
assert.equal(_.title(undefined), '')
})
})

describe('trim function', () => {
test('handles bad input', () => {
assert.equal(_.trim(null), '')
assert.equal(_.trim(undefined), '')
})
test('returns input string correctly trimmed', () => {
assert.equal(_.trim('hello', 'x'), 'hello')
assert.equal(_.trim(' hello '), 'hello')
assert.equal(_.trim(' __hello__ ', '_'), ' __hello__ ')
assert.equal(_.trim('__hello__', '_'), 'hello')
assert.equal(_.trim('//repos////', '/'), 'repos')
assert.equal(_.trim('/repos/:owner/:repo/', '/'), 'repos/:owner/:repo')
})
})
})

0 comments on commit e9ae1b2

Please sign in to comment.