Skip to content

Commit e9ae1b2

Browse files
authored
Add trim function to string module (#171)
* add trim function to string module
1 parent 9d7ce47 commit e9ae1b2

File tree

7 files changed

+54
-5
lines changed

7 files changed

+54
-5
lines changed

Diff for: cdn/radash.esm.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -783,5 +783,11 @@ const template = (str, data, regex = /\{\{(.+?)\}\}/g) => {
783783
return acc.replace(match[0], data[match[1]]);
784784
}, str);
785785
};
786+
const trim = (str, charsToTrim = " ") => {
787+
if (!str)
788+
return "";
789+
const regex = new RegExp(`^[${charsToTrim}]+|[${charsToTrim}]+$`, "g");
790+
return str.replace(regex, "");
791+
};
786792

787-
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 };
793+
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 };

Diff for: cdn/radash.js

+7
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,12 @@ var radash = (function (exports) {
786786
return acc.replace(match[0], data[match[1]]);
787787
}, str);
788788
};
789+
const trim = (str, charsToTrim = " ") => {
790+
if (!str)
791+
return "";
792+
const regex = new RegExp(`^[${charsToTrim}]+|[${charsToTrim}]+$`, "g");
793+
return str.replace(regex, "");
794+
};
789795

790796
exports.alphabetical = alphabetical;
791797
exports.boil = boil;
@@ -865,6 +871,7 @@ var radash = (function (exports) {
865871
exports.toFloat = toFloat;
866872
exports.toInt = toInt;
867873
exports.toggle = toggle;
874+
exports.trim = trim;
868875
exports.try = tryit;
869876
exports.tryit = tryit;
870877
exports.uid = uid;

Diff for: cdn/radash.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "radash",
3-
"version": "9.4.2",
3+
"version": "9.5.0",
44
"description": "Functional utility library - modern, simple, typed, powerful",
55
"main": "dist/cjs/index.cjs",
66
"module": "dist/esm/index.mjs",
@@ -48,4 +48,4 @@
4848
"engines": {
4949
"node": ">=14.18.0"
5050
}
51-
}
51+
}

Diff for: src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ export {
8181
pascal,
8282
snake,
8383
template,
84-
title
84+
title,
85+
trim
8586
} from './string'
8687
export {
8788
isArray,

Diff for: src/string.ts

+20
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,23 @@ export const template = (
116116
return acc.replace(match[0], data[match[1]])
117117
}, str)
118118
}
119+
120+
/**
121+
* Trims all prefix and suffix characters from the given
122+
* string. Like the builtin trim function but accepts
123+
* other characters you would like to trim.
124+
*
125+
* ```typescript
126+
* trim(' hello ') // => 'hello'
127+
* trim('__hello__', '_') // => 'hello'
128+
* trim('/repos/:owner/:repo/', '/') // => 'repos/:owner/:repo'
129+
* ```
130+
*/
131+
export const trim = (
132+
str: string | null | undefined,
133+
charsToTrim: string = ' '
134+
) => {
135+
if (!str) return ''
136+
const regex = new RegExp(`^[${charsToTrim}]+|[${charsToTrim}]+$`, 'g')
137+
return str.replace(regex, '')
138+
}

Diff for: src/tests/string.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,19 @@ describe('string module', () => {
174174
assert.equal(_.title(undefined), '')
175175
})
176176
})
177+
178+
describe('trim function', () => {
179+
test('handles bad input', () => {
180+
assert.equal(_.trim(null), '')
181+
assert.equal(_.trim(undefined), '')
182+
})
183+
test('returns input string correctly trimmed', () => {
184+
assert.equal(_.trim('hello', 'x'), 'hello')
185+
assert.equal(_.trim(' hello '), 'hello')
186+
assert.equal(_.trim(' __hello__ ', '_'), ' __hello__ ')
187+
assert.equal(_.trim('__hello__', '_'), 'hello')
188+
assert.equal(_.trim('//repos////', '/'), 'repos')
189+
assert.equal(_.trim('/repos/:owner/:repo/', '/'), 'repos/:owner/:repo')
190+
})
191+
})
177192
})

0 commit comments

Comments
 (0)