Skip to content

Commit 717f58f

Browse files
committed
Added lodash; Added StringUtils.filename
1 parent 91edd82 commit 717f58f

4 files changed

Lines changed: 46 additions & 3 deletions

File tree

package-lock.json

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
},
1010
"dependencies": {
1111
"axios": "0.19.2",
12-
"immutable": "4.0.0-rc.12"
12+
"immutable": "4.0.0-rc.12",
13+
"lodash": "4.17.15"
1314
},
1415
"description": "Common patterns, functions, etc... used when building react applications",
1516
"devDependencies": {
1617
"@testing-library/jest-dom": "5.5.0",
1718
"@testing-library/react": "10.0.4",
1819
"@types/jest": "25.1.5",
20+
"@types/lodash": "4.14.108",
1921
"@types/node": "13.11.0",
2022
"@types/rosie": "0.0.37",
2123
"jest": "25.5.4",

src/utilities/string-utils.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,32 @@
11
import StringUtils from "./string-utils";
22

33
describe("StringUtils", () => {
4+
// -------------------------------------------------------------------------------------------------
5+
// #region filename
6+
// -------------------------------------------------------------------------------------------------
7+
8+
describe("filename", (): void => {
9+
test.each`
10+
inputString | expected
11+
${null} | ${undefined}
12+
${undefined} | ${undefined}
13+
${""} | ${""}
14+
${"/"} | ${""}
15+
${"path/no-extension"} | ${"no-extension"}
16+
${"path/f.extension"} | ${"f.extension"}
17+
${"multi/level/file.name"} | ${"file.name"}
18+
${"mult1/l3v3l/f1l3.nam3"} | ${"f1l3.nam3"}
19+
${"mult1/l3v3l/file with.spaces"} | ${"file with.spaces"}
20+
`(
21+
`when inputString is $inputString, returns $expected`,
22+
({ inputString, expected }: any): void => {
23+
expect(StringUtils.filename(inputString)).toBe(expected);
24+
}
25+
);
26+
});
27+
28+
//#endregion filename
29+
430
// -------------------------------------------------------------------------------------------------
531
// #region hasValue
632
// -------------------------------------------------------------------------------------------------

src/utilities/string-utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import _ from "lodash";
2+
13
// -----------------------------------------------------------------------------------------
24
// #region Constants
35
// -----------------------------------------------------------------------------------------
@@ -10,6 +12,13 @@ const REGEX_VALID_EMAIL = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(
1012
// #region Functions
1113
// -----------------------------------------------------------------------------------------
1214

15+
/**
16+
* Returns the filename from the supplied string, including extension
17+
* @param value
18+
*/
19+
const filename = (value?: string): string | undefined =>
20+
value?.split("/").pop();
21+
1322
/**
1423
* Determines whether or not the provided value is NOT `undefined`, `null`, or an empty string
1524
* (after trimming both ends of the string)
@@ -58,6 +67,7 @@ const truncateRight = (value: string, truncateAtPos: number): string => {
5867
// -----------------------------------------------------------------------------------------
5968

6069
const StringUtils = {
70+
filename,
6171
hasValue,
6272
isEmpty,
6373
isValidEmail,

0 commit comments

Comments
 (0)