Skip to content

Commit 90c7f79

Browse files
authoredMar 15, 2020
Merge pull request #11 from rpearce/ts
convert to typescript
2 parents b34dbc4 + d1e8596 commit 90c7f79

19 files changed

+4039
-2601
lines changed
 

‎.eslintignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
!.eslintrc.js
2+
!.prettierrc.js
3+
dist/

‎.eslintrc.js

+25-9
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,44 @@ module.exports = {
22
env: {
33
browser: true,
44
es6: true,
5+
jest: true,
56
node: true
67
},
78
extends: [
8-
'eslint:recommended',
9+
'plugin:react/recommended',
10+
'plugin:@typescript-eslint/recommended',
11+
'prettier/@typescript-eslint',
912
'plugin:prettier/recommended',
10-
'plugin:react/recommended'
13+
'plugin:jsx-a11y/recommended'
1114
],
1215
parserOptions: {
13-
ecmaVersion: 2019,
16+
ecmaVersion: 2020,
1417
sourceType: 'module'
1518
},
16-
plugins: [],
19+
parser: '@typescript-eslint/parser',
20+
plugins: ['jsx-a11y', 'react', '@typescript-eslint'],
1721
rules: {
18-
'indent': ['error', 2, { 'SwitchCase': 1 }],
22+
'@typescript-eslint/no-unused-vars': 'error',
23+
'jsx-quotes': ['error', 'prefer-double'],
24+
'jsx-a11y/no-onchange': 0,
1925
'no-trailing-spaces': 'error',
2026
'object-curly-spacing': ['error', 'always'],
21-
'quotes': ['error', 'single', { 'allowTemplateLiterals': true }],
22-
'semi': ['error', 'never']
27+
quotes: ['error', 'single', { allowTemplateLiterals: true }],
28+
'react/prop-types': 0,
29+
semi: ['error', 'never']
2330
},
2431
settings: {
2532
react: {
26-
version: '16'
33+
version: 'detect'
2734
}
28-
}
35+
},
36+
overrides: [
37+
{
38+
files: ['*.js', '*.jsx'],
39+
rules: {
40+
'@typescript-eslint/explicit-function-return-type': 'off',
41+
'@typescript-eslint/no-var-requires': 'off'
42+
}
43+
}
44+
]
2945
}

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,6 @@ typings/
6060
# next.js build output
6161
.next
6262

63+
dist/
6364
*.tmp
65+
.DS_Store

‎.travis.yml

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
language: node_js
22
node_js:
3-
- "12"
3+
- "13"
4+
before_install:
5+
- curl -o- -L https://yarnpkg.com/install.sh | bash
6+
- export PATH="$HOME/.yarn/bin:$PATH"
47
cache: yarn
8+
script:
9+
- yarn ci
510
after_success:
6-
- npm run coverage
11+
- yarn run coverage

‎CHANGELOG.md

+11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [1.0.0] - 2020-03-15
8+
9+
### Added
10+
* TypeScript support
11+
12+
### Changed
13+
* License from ISC to BSD-3
14+
15+
### Fixed
16+
* security issues in dependencies
17+
718
## [0.1.3] - 2019-12-28
819

920
### Changed

‎LICENSE

+27-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
1-
ISC License (ISC)
1+
Copyright (c) 2020, Robert Pearce
22

3-
Copyright 2019 Robert Pearce
3+
All rights reserved.
44

5-
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
67

7-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
8+
* Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
11+
* Redistributions in binary form must reproduce the above
12+
copyright notice, this list of conditions and the following
13+
disclaimer in the documentation and/or other materials provided
14+
with the distribution.
15+
16+
* Neither the name of Robert Pearce nor the names of other
17+
contributors may be used to endorse or promote products derived
18+
from this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

‎__tests__/index.tsx

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import React from 'react'
2+
import flexibleStringReplace from '../source'
3+
4+
test('flexibleStringReplace is defined', () => {
5+
expect(flexibleStringReplace).toBeDefined()
6+
})
7+
8+
test('string pattern & string replacer for first match', () => {
9+
const str = 'The rain in Spain falls mainly on the plain. Spain is nice.'
10+
const pattern = 'Spain'
11+
const replacer = 'foo bar baz'
12+
const res = flexibleStringReplace(pattern, replacer, str)
13+
14+
expect(res).toEqual([
15+
'The rain in ',
16+
'foo bar baz',
17+
' falls mainly on the plain. Spain is nice.'
18+
])
19+
})
20+
21+
test('string pattern & html interpolation for first match', () => {
22+
const str = 'The rain in Spain falls mainly on the plain. Spain is nice.'
23+
const pattern = 'Spain'
24+
const replacer = (match: string): string => `<mark>${match}</mark>`
25+
const res = flexibleStringReplace(pattern, replacer, str)
26+
27+
expect(res).toEqual([
28+
'The rain in ',
29+
'<mark>Spain</mark>',
30+
' falls mainly on the plain. Spain is nice.'
31+
])
32+
})
33+
34+
test('regexp pattern & html string interpolation function with matches', () => {
35+
const str = 'The rain in Spain falls mainly on the plain. Spain is nice.'
36+
const pattern = new RegExp('Spain', 'igm')
37+
const replacer = (match: string): string => `<mark>${match}</mark>`
38+
const res = flexibleStringReplace(pattern, replacer, str)
39+
40+
expect(res).toEqual([
41+
'The rain in ',
42+
'<mark>Spain</mark>',
43+
' falls mainly on the plain. ',
44+
'<mark>Spain</mark>',
45+
' is nice.'
46+
])
47+
})
48+
49+
test('forwards matching group parts in replacer params', () => {
50+
const str = 'Dear @diary, I hope this test #passes #pray'
51+
const pattern = /\s(@\w+)|\s(#\w+)/gm
52+
const ats: string[] = []
53+
const tags: string[] = []
54+
const replacer = (match: string, at: string, tag: string): void => {
55+
if (at) {
56+
ats.push(at)
57+
}
58+
if (tag) {
59+
tags.push(tag)
60+
}
61+
}
62+
flexibleStringReplace(pattern, replacer, str)
63+
64+
expect(ats).toEqual(['@diary'])
65+
expect(tags).toEqual(['#passes', '#pray'])
66+
})
67+
68+
test('regexp pattern & react interpolation function', () => {
69+
const str = 'The rain in Spain falls mainly on the plain. Spain is nice.'
70+
const pattern = new RegExp('Spain', 'igm')
71+
const replacer = (match: string, offset: string): JSX.Element => (
72+
<mark key={offset}>{match}</mark>
73+
)
74+
const res = flexibleStringReplace(pattern, replacer, str)
75+
76+
expect(res).toEqual([
77+
'The rain in ',
78+
<mark key={12}>Spain</mark>,
79+
' falls mainly on the plain. ',
80+
<mark key={45}>Spain</mark>,
81+
' is nice.'
82+
])
83+
})

‎ava.config.js

-7
This file was deleted.

‎index.js

-31
This file was deleted.

‎jest.config.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
clearMocks: true,
3+
collectCoverage: true,
4+
collectCoverageFrom: ['<rootDir>/source/**/*.{ts,tsx}'],
5+
coveragePathIgnorePatterns: ['/node_modules/', '<rootDir>/source/@types'],
6+
moduleNameMapper: {},
7+
preset: 'ts-jest',
8+
setupFilesAfterEnv: [],
9+
verbose: true
10+
}

‎lint-staged.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module.exports = {
2-
'*.js': ['eslint --fix', 'git add']
2+
'*.{js,ts,tsx}': ['eslint --fix']
33
}

‎nyc.config.js

-3
This file was deleted.

‎package.json

+47-26
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
{
22
"name": "@rpearce/flexible-string-replace",
3-
"version": "0.1.3",
3+
"version": "1.0.0",
44
"description": "🧶 Safely replace any part of a string with anything. Example: useful for replacing substrings with JSX in React",
5-
"main": "index.js",
5+
"main": "dist/cjs/index.js",
6+
"module": "dist/esm/index.js",
67
"repository": {
78
"type": "git",
89
"url": "git@github.com:rpearce/flexible-string-replace.git"
910
},
1011
"homepage": "https://github.com/rpearce/flexible-string-replace",
1112
"bugs": "https://github.com/rpearce/flexible-string-replace/issues",
1213
"author": "Robert Pearce <me@robertwpearce.com>",
13-
"contributors": [
14-
"Robert Pearce <me@robertwpearce.com> (https://robertwpearce.com)"
15-
],
16-
"license": "ISC",
14+
"license": "BSD-3",
1715
"keywords": [
1816
"string-replace",
1917
"string-manipulation",
@@ -34,32 +32,55 @@
3432
"AUTHORS",
3533
"LICENSE",
3634
"README.md",
37-
"index.js"
35+
"dist/"
3836
],
3937
"sideEffects": false,
4038
"scripts": {
39+
"build": "run-s clean build:js",
40+
"build:js": "rollup -c ./rollup.config.js",
41+
"build:js:watch": "yarn build:js -- -w",
42+
"ci": "run-p lint build test",
43+
"clean": "run-p clean:dist",
44+
"clean:dist": "rm -rf ./dist",
4145
"contributors:add": "all-contributors add",
4246
"contributors:generate": "all-contributors generate",
43-
"coverage": "nyc report --reporter=text-lcov | coveralls",
44-
"coverage:report": "nyc report --reporter=lcov && echo \"view coverage: ./coverage/lcov-report/index.html\"",
45-
"lint": "eslint .",
46-
"prepublishOnly": "run-p lint test",
47-
"test": "nyc ava --verbose"
47+
"coverage": "jest --coverage --coverageReporters=text-lcov | coveralls",
48+
"lint": "eslint . --ext .js,.ts,.tsx",
49+
"prepublishOnly": "run-p lint test && yarn build:js",
50+
"test": "jest"
4851
},
4952
"devDependencies": {
50-
"@babel/preset-react": "^7.7.4",
51-
"all-contributors-cli": "^6.11.2",
52-
"ava": "^2.4.0",
53-
"coveralls": "^3.0.9",
54-
"eslint": "^6.8.0",
55-
"eslint-config-prettier": "^6.9.0",
56-
"eslint-plugin-prettier": "^3.1.2",
57-
"eslint-plugin-react": "^7.17.0",
58-
"husky": "^3.1.0",
59-
"lint-staged": "^9.5.0",
60-
"npm-run-all": "^4.1.5",
61-
"nyc": "^15.0.0",
62-
"prettier": "^1.19.1",
63-
"react": "^16.12.0"
53+
"@rollup/plugin-commonjs": "11.0.2",
54+
"@rollup/plugin-node-resolve": "7.1.1",
55+
"@testing-library/jest-dom": "5.1.1",
56+
"@testing-library/react": "10.0.1",
57+
"@types/jest": "25.1.4",
58+
"@types/node": "13.9.1",
59+
"@types/react": "16.9.23",
60+
"@types/react-dom": "16.9.5",
61+
"@typescript-eslint/eslint-plugin": "2.23.0",
62+
"@typescript-eslint/parser": "2.23.0",
63+
"all-contributors-cli": "6.14.0",
64+
"coveralls": "3.0.9",
65+
"eslint": "6.8.0",
66+
"eslint-config-prettier": "6.10.0",
67+
"eslint-plugin-jest": "23.8.2",
68+
"eslint-plugin-jsx-a11y": "6.2.3",
69+
"eslint-plugin-prettier": "3.1.2",
70+
"eslint-plugin-react": "7.19.0",
71+
"husky": "4.2.3",
72+
"jest": "25.1.0",
73+
"lint-staged": "10.0.8",
74+
"npm-run-all": "4.1.5",
75+
"prettier": "1.19.1",
76+
"react": "16.13.0",
77+
"react-dom": "16.13.0",
78+
"rollup": "2.0.6",
79+
"rollup-plugin-postcss": "2.4.1",
80+
"rollup-plugin-terser": "5.3.0",
81+
"rollup-plugin-typescript2": "0.26.0",
82+
"ts-jest": "25.2.1",
83+
"tslib": "1.11.1",
84+
"typescript": "3.8.3"
6485
}
6586
}

‎rollup.config.js

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import commonjs from '@rollup/plugin-commonjs'
2+
import resolve from '@rollup/plugin-node-resolve'
3+
import { dirname } from 'path'
4+
import { terser } from 'rollup-plugin-terser'
5+
import typescript from 'rollup-plugin-typescript2'
6+
import pkg from './package.json'
7+
8+
const esm = [
9+
// ESModules (esm) build
10+
{
11+
input: './source/index.ts',
12+
output: {
13+
dir: dirname(pkg.module),
14+
exports: 'named',
15+
format: 'esm',
16+
name: 'fsr-esm',
17+
sourcemap: false
18+
},
19+
external: [],
20+
plugins: [resolve(), typescript({ tsconfig: './tsconfig.build.json' })]
21+
}
22+
]
23+
24+
const cjs = [
25+
// CommonJS (cjs) build
26+
{
27+
input: './source/index.ts',
28+
output: {
29+
dir: dirname(pkg.main),
30+
exports: 'named',
31+
format: 'cjs',
32+
globals: {},
33+
name: 'fsr-cjs',
34+
sourcemap: false
35+
},
36+
external: [],
37+
plugins: [
38+
resolve(),
39+
commonjs({}),
40+
typescript({ tsconfig: './tsconfig.build.json' })
41+
]
42+
},
43+
44+
// Minified cjs build
45+
{
46+
input: './source/index.ts',
47+
output: {
48+
file: `${dirname(pkg.main)}/${pkg.name}.min.js`,
49+
exports: 'named',
50+
format: 'cjs',
51+
name: 'fsr-cjs-min',
52+
sourcemap: false
53+
},
54+
external: [],
55+
plugins: [
56+
resolve(),
57+
commonjs({}),
58+
typescript({ tsconfig: './tsconfig.build.json' }),
59+
terser()
60+
]
61+
}
62+
]
63+
64+
const umd = [
65+
// Universal module definition (UMD) build
66+
{
67+
input: './source/index.ts',
68+
output: {
69+
file: './dist/umd/flexible-string-replace.js',
70+
exports: 'named',
71+
format: 'umd',
72+
globals: {},
73+
name: 'fsr-umd',
74+
sourcemap: false
75+
},
76+
external: [],
77+
plugins: [
78+
resolve(),
79+
commonjs({}),
80+
typescript({ tsconfig: './tsconfig.build.json' })
81+
]
82+
},
83+
84+
// Minified (UMD) build
85+
{
86+
input: './source/index.ts',
87+
output: {
88+
file: './dist/umd/flexible-string-replace.min.js',
89+
exports: 'named',
90+
format: 'umd',
91+
globals: {},
92+
name: 'fsr-umd',
93+
sourcemap: false
94+
},
95+
external: [],
96+
plugins: [
97+
resolve(),
98+
commonjs({}),
99+
typescript({ tsconfig: './tsconfig.build.json' }),
100+
terser()
101+
]
102+
}
103+
]
104+
105+
let config
106+
107+
switch (process.env.BUILD_ENV) {
108+
case 'cjs':
109+
config = cjs
110+
break
111+
case 'esm':
112+
config = esm
113+
break
114+
case 'umd':
115+
config = umd
116+
break
117+
default:
118+
config = cjs.concat(esm).concat(umd)
119+
}
120+
121+
export default config

‎source/index.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export default function flexibleStringReplace(
2+
pattern: string | RegExp,
3+
replacer: string | ((...args: any[]) => any), // eslint-disable-line @typescript-eslint/no-explicit-any
4+
str: string
5+
): string[] {
6+
const result = []
7+
let position = 0
8+
9+
str.replace(pattern, function(...args): string {
10+
const argsN = args.length
11+
const match = args[0]
12+
const originalStr = args[argsN - 1]
13+
const charOffset = args[argsN - 2]
14+
const prevChars = originalStr.slice(position, charOffset)
15+
const replaced =
16+
typeof replacer === 'function' ? replacer(...args) : replacer
17+
18+
result.push(prevChars, replaced)
19+
position = charOffset + match.length
20+
21+
return '' // no-op
22+
})
23+
24+
result.push(str.slice(position))
25+
26+
return result
27+
}

‎test.js

-88
This file was deleted.

‎tsconfig.build.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"exclude": ["./__tests__/**/*"]
4+
}

‎tsconfig.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"allowJs": false,
4+
"allowSyntheticDefaultImports": true,
5+
"declaration": true,
6+
"esModuleInterop": true,
7+
"jsx": "react",
8+
"lib": ["es2020", "dom"],
9+
"module": "es6",
10+
"moduleResolution": "node",
11+
"noImplicitAny": true,
12+
"outDir": "./dist/",
13+
"sourceMap": true,
14+
"strict": true,
15+
"target": "es5"
16+
},
17+
"exclude": ["node_modules/"],
18+
"include": ["./source/**/*", "./__tests__/**/*"]
19+
}

‎yarn.lock

+3,652-2,430
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.