-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: @rocket.chat/string-helpers (#431)
- Loading branch information
Showing
84 changed files
with
830 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
-268 Bytes
(99%)
...-kit/.loki/reference/chrome_iphone7_Surfaces_Modal_Actions_With_All_Selects.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-265 Bytes
(98%)
...ge-ui-kit/.loki/reference/chrome_iphone7_Surfaces_Modal_Actions_With_Button.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-252 Bytes
(99%)
...-kit/.loki/reference/chrome_iphone7_Surfaces_Modal_Actions_With_Date_Picker.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-264 Bytes
(98%)
...ce/chrome_iphone7_Surfaces_Modal_Actions_With_Filtered_Conversations_Select.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-264 Bytes
(98%)
...ki/reference/chrome_iphone7_Surfaces_Modal_Actions_With_Initialized_Selects.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-244 Bytes
(99%)
...lage-ui-kit/.loki/reference/chrome_iphone7_Surfaces_Modal_Conditional_Block.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-272 Bytes
(99%)
...ge-ui-kit/.loki/reference/chrome_iphone7_Surfaces_Modal_Context_With_Mrkdwn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-272 Bytes
(99%)
...i-kit/.loki/reference/chrome_iphone7_Surfaces_Modal_Context_With_Plain_Text.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-295 Bytes
(99%)
.../.loki/reference/chrome_iphone7_Surfaces_Modal_Context_With_Text_And_Images.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-262 Bytes
(98%)
packages/fuselage-ui-kit/.loki/reference/chrome_iphone7_Surfaces_Modal_Divider.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-268 Bytes
(100%)
...elage-ui-kit/.loki/reference/chrome_iphone7_Surfaces_Modal_Image_With_Title.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-178 Bytes
(100%)
...ge-ui-kit/.loki/reference/chrome_iphone7_Surfaces_Modal_Image_Without_Title.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-264 Bytes
(99%)
...ui-kit/.loki/reference/chrome_iphone7_Surfaces_Modal_Input_With_Date_Picker.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-260 Bytes
(99%)
...i-kit/.loki/reference/chrome_iphone7_Surfaces_Modal_Input_With_Linear_Scale.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-276 Bytes
(98%)
....loki/reference/chrome_iphone7_Surfaces_Modal_Input_With_Multi_Users_Select.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-241 Bytes
(99%)
...ference/chrome_iphone7_Surfaces_Modal_Input_With_Multiline_Plain_Text_Input.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-255 Bytes
(99%)
...t/.loki/reference/chrome_iphone7_Surfaces_Modal_Input_With_Plain_Text_Input.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-261 Bytes
(99%)
...-kit/.loki/reference/chrome_iphone7_Surfaces_Modal_Input_With_Static_Select.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-259 Bytes
(99%)
....loki/reference/chrome_iphone7_Surfaces_Modal_Section_With_Button_Accessory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-248 Bytes
(99%)
.../reference/chrome_iphone7_Surfaces_Modal_Section_With_Date_Picker_Accessory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-286 Bytes
(100%)
.../.loki/reference/chrome_iphone7_Surfaces_Modal_Section_With_Image_Accessory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-271 Bytes
(99%)
...ge-ui-kit/.loki/reference/chrome_iphone7_Surfaces_Modal_Section_With_Mrkdwn.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-269 Bytes
(99%)
...eference/chrome_iphone7_Surfaces_Modal_Section_With_Overflow_Menu_Accessory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-244 Bytes
(99%)
...i-kit/.loki/reference/chrome_iphone7_Surfaces_Modal_Section_With_Plain_Text.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-245 Bytes
(99%)
...-kit/.loki/reference/chrome_iphone7_Surfaces_Modal_Section_With_Text_Fields.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-68 Bytes
(99%)
...i-kit/.loki/reference/chrome_laptop_Surfaces_Modal_Actions_With_All_Selects.png
Oops, something went wrong.
Binary file modified
BIN
-66 Bytes
(99%)
...age-ui-kit/.loki/reference/chrome_laptop_Surfaces_Modal_Actions_With_Button.png
Oops, something went wrong.
Binary file modified
BIN
-72 Bytes
(99%)
...i-kit/.loki/reference/chrome_laptop_Surfaces_Modal_Actions_With_Date_Picker.png
Oops, something went wrong.
Binary file modified
BIN
-70 Bytes
(99%)
...nce/chrome_laptop_Surfaces_Modal_Actions_With_Filtered_Conversations_Select.png
Oops, something went wrong.
Binary file modified
BIN
-70 Bytes
(99%)
...oki/reference/chrome_laptop_Surfaces_Modal_Actions_With_Initialized_Selects.png
Oops, something went wrong.
Binary file modified
BIN
-70 Bytes
(99%)
...elage-ui-kit/.loki/reference/chrome_laptop_Surfaces_Modal_Conditional_Block.png
Oops, something went wrong.
Binary file modified
BIN
-64 Bytes
(99%)
...age-ui-kit/.loki/reference/chrome_laptop_Surfaces_Modal_Context_With_Mrkdwn.png
Oops, something went wrong.
Binary file modified
BIN
-65 Bytes
(99%)
...ui-kit/.loki/reference/chrome_laptop_Surfaces_Modal_Context_With_Plain_Text.png
Oops, something went wrong.
Binary file modified
BIN
-67 Bytes
(100%)
...t/.loki/reference/chrome_laptop_Surfaces_Modal_Context_With_Text_And_Images.png
Oops, something went wrong.
Binary file modified
BIN
-67 Bytes
(99%)
packages/fuselage-ui-kit/.loki/reference/chrome_laptop_Surfaces_Modal_Divider.png
Oops, something went wrong.
Binary file modified
BIN
-68 Bytes
(100%)
...selage-ui-kit/.loki/reference/chrome_laptop_Surfaces_Modal_Image_With_Title.png
Oops, something went wrong.
Binary file modified
BIN
-86 Bytes
(100%)
...age-ui-kit/.loki/reference/chrome_laptop_Surfaces_Modal_Image_Without_Title.png
Oops, something went wrong.
Binary file modified
BIN
-68 Bytes
(99%)
...-ui-kit/.loki/reference/chrome_laptop_Surfaces_Modal_Input_With_Date_Picker.png
Oops, something went wrong.
Binary file modified
BIN
-70 Bytes
(99%)
...ui-kit/.loki/reference/chrome_laptop_Surfaces_Modal_Input_With_Linear_Scale.png
Oops, something went wrong.
Binary file modified
BIN
-64 Bytes
(99%)
.../.loki/reference/chrome_laptop_Surfaces_Modal_Input_With_Multi_Users_Select.png
Oops, something went wrong.
Binary file modified
BIN
-65 Bytes
(99%)
...eference/chrome_laptop_Surfaces_Modal_Input_With_Multiline_Plain_Text_Input.png
Oops, something went wrong.
Binary file modified
BIN
-64 Bytes
(99%)
...it/.loki/reference/chrome_laptop_Surfaces_Modal_Input_With_Plain_Text_Input.png
Oops, something went wrong.
Binary file modified
BIN
-61 Bytes
(99%)
...i-kit/.loki/reference/chrome_laptop_Surfaces_Modal_Input_With_Static_Select.png
Oops, something went wrong.
Binary file modified
BIN
-65 Bytes
(99%)
.../.loki/reference/chrome_laptop_Surfaces_Modal_Section_With_Button_Accessory.png
Oops, something went wrong.
Binary file modified
BIN
-65 Bytes
(99%)
...i/reference/chrome_laptop_Surfaces_Modal_Section_With_Date_Picker_Accessory.png
Oops, something went wrong.
Binary file modified
BIN
-71 Bytes
(100%)
...t/.loki/reference/chrome_laptop_Surfaces_Modal_Section_With_Image_Accessory.png
Oops, something went wrong.
Binary file modified
BIN
-47 Bytes
(100%)
...age-ui-kit/.loki/reference/chrome_laptop_Surfaces_Modal_Section_With_Mrkdwn.png
Oops, something went wrong.
Binary file modified
BIN
-66 Bytes
(99%)
...reference/chrome_laptop_Surfaces_Modal_Section_With_Overflow_Menu_Accessory.png
Oops, something went wrong.
Binary file modified
BIN
-70 Bytes
(99%)
...ui-kit/.loki/reference/chrome_laptop_Surfaces_Modal_Section_With_Plain_Text.png
Oops, something went wrong.
Binary file modified
BIN
-78 Bytes
(100%)
...i-kit/.loki/reference/chrome_laptop_Surfaces_Modal_Section_With_Text_Fields.png
Oops, something went wrong.
Binary file modified
BIN
-817 Bytes
(100%)
packages/fuselage/.loki/reference/chrome_iphone7_Misc_Icon_Default.png
Oops, something went wrong.
Binary file modified
BIN
+464 Bytes
(100%)
packages/fuselage/.loki/reference/chrome_laptop_Misc_Icon_Default.png
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/node_modules | ||
/dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
module.exports = { | ||
extends: [ | ||
'plugin:@typescript-eslint/recommended', | ||
'plugin:@typescript-eslint/eslint-recommended', | ||
'@rocket.chat/eslint-config', | ||
'prettier', | ||
], | ||
parser: '@typescript-eslint/parser', | ||
plugins: ['@typescript-eslint', 'prettier'], | ||
rules: { | ||
'@typescript-eslint/ban-ts-ignore': 'off', | ||
'@typescript-eslint/indent': 'off', | ||
'@typescript-eslint/no-extra-parens': 'off', | ||
'@typescript-eslint/no-explicit-any': 'off', | ||
'@typescript-eslint/no-unused-vars': [ | ||
'error', | ||
{ | ||
argsIgnorePattern: '^_', | ||
}, | ||
], | ||
'func-call-spacing': 'off', | ||
'indent': 'off', | ||
'import/order': [ | ||
'error', | ||
{ | ||
'newlines-between': 'always', | ||
'groups': [ | ||
'builtin', | ||
'external', | ||
'internal', | ||
['parent', 'sibling', 'index'], | ||
], | ||
'alphabetize': { | ||
order: 'asc', | ||
}, | ||
}, | ||
], | ||
'jsx-quotes': ['error', 'prefer-single'], | ||
'no-extra-parens': 'off', | ||
'no-spaced-func': 'off', | ||
'no-unused-vars': 'off', | ||
'no-useless-constructor': 'off', | ||
'no-use-before-define': 'off', | ||
'prefer-arrow-callback': ['error', { allowNamedFunctions: true }], | ||
'prettier/prettier': 2, | ||
}, | ||
env: { | ||
browser: true, | ||
es6: true, | ||
jest: true, | ||
}, | ||
settings: { | ||
'import/resolver': { | ||
node: { | ||
extensions: ['.js', '.ts', '.tsx'], | ||
}, | ||
}, | ||
'react': { | ||
version: 'detect', | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/node_modules | ||
/dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"singleQuote": true, | ||
"semi": true, | ||
"quoteProps": "consistent", | ||
"jsxSingleQuote": true, | ||
"printWidth": 80 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
errorOnDeprecated: true, | ||
testMatch: ['<rootDir>/src/**/*.spec.[jt]s?(x)'], | ||
globals: { | ||
'ts-jest': { | ||
tsconfig: { | ||
noUnusedLocals: false, | ||
noUnusedParameters: false, | ||
}, | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
{ | ||
"name": "@rocket.chat/string-helpers", | ||
"version": "0.23.0", | ||
"description": "Helper functions for string manipulation", | ||
"keywords": [ | ||
"string", | ||
"helpers" | ||
], | ||
"author": { | ||
"name": "Rocket.Chat", | ||
"url": "https://rocket.chat/" | ||
}, | ||
"homepage": "https://github.com/RocketChat/Rocket.Chat.Fuselage#readme", | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/RocketChat/Rocket.Chat.Fuselage.git" | ||
}, | ||
"main": "dist/cjs/index.js", | ||
"module": "dist/esm/index.js", | ||
"types": "dist/esm/index.d.ts", | ||
"typesVersions": { | ||
"<4.1": { | ||
"*": [ | ||
"ts3.4/*" | ||
] | ||
} | ||
}, | ||
"files": [ | ||
"/dist" | ||
], | ||
"scripts": { | ||
"build": "run-s .:build:clean .:build:esm .:build:cjs .:build:ts3.4", | ||
".:build:clean": "rimraf dist", | ||
".:build:esm": "tsc -p tsconfig.json", | ||
".:build:cjs": "tsc -p tsconfig-cjs.json", | ||
".:build:ts3.4": "downlevel-dts dist/esm/ dist/ts3.4/ --to=3.4", | ||
"lint": "eslint src", | ||
"lint-fix": "eslint --fix src", | ||
"lint-staged": "lint-staged", | ||
"test": "jest --runInBand" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/RocketChat/Rocket.Chat.Fuselage/issues" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^26.0.22", | ||
"@typescript-eslint/eslint-plugin": "^4.22.0", | ||
"@typescript-eslint/parser": "^4.22.0", | ||
"downlevel-dts": "^0.7.0", | ||
"eslint": "^7.24.0", | ||
"eslint-config-prettier": "^8.2.0", | ||
"eslint-plugin-import": "^2.22.1", | ||
"eslint-plugin-prettier": "^3.4.0", | ||
"jest": "^26.6.3", | ||
"lint-staged": "^10.5.4", | ||
"npm-run-all": "^4.1.5", | ||
"prettier": "^2.2.1", | ||
"rimraf": "^3.0.2", | ||
"ts-jest": "^26.5.5", | ||
"typescript": "^4.2.4" | ||
}, | ||
"dependencies": { | ||
"tslib": "^2.2.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { capitalize } from './capitalize'; | ||
|
||
describe('capitalize', () => { | ||
it('should convert "xyz" to "Xyz"', () => { | ||
expect(capitalize('xyz')).toBe('Xyz'); | ||
}); | ||
|
||
it('should convert "xyz xyz" to "Xyz xyz"', () => { | ||
expect(capitalize('xyz xyz')).toBe('Xyz xyz'); | ||
}); | ||
|
||
it('should convert " xyz" to " xyz"', () => { | ||
expect(capitalize(' xyz')).toBe(' xyz'); | ||
}); | ||
|
||
it('should convert undefined to ""', () => { | ||
expect(capitalize((undefined as unknown) as string)).toBe(''); | ||
}); | ||
|
||
it('should convert null to ""', () => { | ||
expect(capitalize((null as unknown) as string)).toBe(''); | ||
}); | ||
|
||
it('should convert false to ""', () => { | ||
expect(capitalize((false as unknown) as string)).toBe(''); | ||
}); | ||
|
||
it('should convert true to ""', () => { | ||
expect(capitalize((true as unknown) as string)).toBe(''); | ||
}); | ||
|
||
it('should convert 0 to ""', () => { | ||
expect(capitalize((0 as unknown) as string)).toBe(''); | ||
}); | ||
|
||
it('should convert 1 to ""', () => { | ||
expect(capitalize((1 as unknown) as string)).toBe(''); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const capitalize = (s: string): string => { | ||
if (typeof s !== 'string') { | ||
return ''; | ||
} | ||
|
||
return s.charAt(0).toUpperCase() + s.slice(1); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { escapeHTML } from './escapeHTML'; | ||
|
||
describe('escapeHTML', () => { | ||
it('works', () => { | ||
expect(escapeHTML('<div>Blah & "blah" & \'blah\'</div>')).toBe( | ||
'<div>Blah & "blah" & 'blah'</div>' | ||
); | ||
expect(escapeHTML('<')).toBe('&lt;'); | ||
expect(escapeHTML(' ')).toBe(' '); | ||
expect(escapeHTML('¢')).toBe('¢'); | ||
expect(escapeHTML('¢ £ ¥ € © ®')).toBe( | ||
'¢ £ ¥ € © ®' | ||
); | ||
expect(escapeHTML((5 as unknown) as string)).toBe('5'); | ||
expect(escapeHTML('')).toBe(''); | ||
expect(escapeHTML((null as unknown) as string)).toBe(''); | ||
expect(escapeHTML((undefined as unknown) as string)).toBe(''); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const characterToHtmlEntityCode = { | ||
'¢': 'cent', | ||
'£': 'pound', | ||
'¥': 'yen', | ||
'€': 'euro', | ||
'©': 'copy', | ||
'®': 'reg', | ||
'<': 'lt', | ||
'>': 'gt', | ||
'"': 'quot', | ||
'&': 'amp', | ||
"'": '#39', | ||
} as const; | ||
|
||
const regex = new RegExp( | ||
`[${Object.keys(characterToHtmlEntityCode).join('')}]`, | ||
'g' | ||
); | ||
|
||
const toString = (object: unknown): string => (object ? `${object}` : ''); | ||
|
||
const isEscapable = ( | ||
char: string | ||
): char is keyof typeof characterToHtmlEntityCode => | ||
char in characterToHtmlEntityCode; | ||
|
||
const escapeChar = (char: string): string => | ||
isEscapable(char) ? `&${characterToHtmlEntityCode[char]};` : ''; | ||
|
||
export const escapeHTML = (str: string): string => | ||
toString(str).replace(regex, escapeChar); |
Oops, something went wrong.