Skip to content

Commit

Permalink
Merge pull request #11 from rpearce/ts
Browse files Browse the repository at this point in the history
convert to typescript
  • Loading branch information
rpearce authored Mar 15, 2020
2 parents b34dbc4 + d1e8596 commit 90c7f79
Show file tree
Hide file tree
Showing 19 changed files with 4,039 additions and 2,601 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
!.eslintrc.js
!.prettierrc.js
dist/
34 changes: 25 additions & 9 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,44 @@ module.exports = {
env: {
browser: true,
es6: true,
jest: true,
node: true
},
extends: [
'eslint:recommended',
'plugin:react/recommended',
'plugin:@typescript-eslint/recommended',
'prettier/@typescript-eslint',
'plugin:prettier/recommended',
'plugin:react/recommended'
'plugin:jsx-a11y/recommended'
],
parserOptions: {
ecmaVersion: 2019,
ecmaVersion: 2020,
sourceType: 'module'
},
plugins: [],
parser: '@typescript-eslint/parser',
plugins: ['jsx-a11y', 'react', '@typescript-eslint'],
rules: {
'indent': ['error', 2, { 'SwitchCase': 1 }],
'@typescript-eslint/no-unused-vars': 'error',
'jsx-quotes': ['error', 'prefer-double'],
'jsx-a11y/no-onchange': 0,
'no-trailing-spaces': 'error',
'object-curly-spacing': ['error', 'always'],
'quotes': ['error', 'single', { 'allowTemplateLiterals': true }],
'semi': ['error', 'never']
quotes: ['error', 'single', { allowTemplateLiterals: true }],
'react/prop-types': 0,
semi: ['error', 'never']
},
settings: {
react: {
version: '16'
version: 'detect'
}
}
},
overrides: [
{
files: ['*.js', '*.jsx'],
rules: {
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-var-requires': 'off'
}
}
]
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ typings/
# next.js build output
.next

dist/
*.tmp
.DS_Store
9 changes: 7 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
language: node_js
node_js:
- "12"
- "13"
before_install:
- curl -o- -L https://yarnpkg.com/install.sh | bash
- export PATH="$HOME/.yarn/bin:$PATH"
cache: yarn
script:
- yarn ci
after_success:
- npm run coverage
- yarn run coverage
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [1.0.0] - 2020-03-15

### Added
* TypeScript support

### Changed
* License from ISC to BSD-3

### Fixed
* security issues in dependencies

## [0.1.3] - 2019-12-28

### Changed
Expand Down
31 changes: 27 additions & 4 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
ISC License (ISC)
Copyright (c) 2020, Robert Pearce

Copyright 2019 Robert Pearce
All rights reserved.

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.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

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.
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

* Neither the name of Robert Pearce nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
83 changes: 83 additions & 0 deletions __tests__/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import React from 'react'
import flexibleStringReplace from '../source'

test('flexibleStringReplace is defined', () => {
expect(flexibleStringReplace).toBeDefined()
})

test('string pattern & string replacer for first match', () => {
const str = 'The rain in Spain falls mainly on the plain. Spain is nice.'
const pattern = 'Spain'
const replacer = 'foo bar baz'
const res = flexibleStringReplace(pattern, replacer, str)

expect(res).toEqual([
'The rain in ',
'foo bar baz',
' falls mainly on the plain. Spain is nice.'
])
})

test('string pattern & html interpolation for first match', () => {
const str = 'The rain in Spain falls mainly on the plain. Spain is nice.'
const pattern = 'Spain'
const replacer = (match: string): string => `<mark>${match}</mark>`
const res = flexibleStringReplace(pattern, replacer, str)

expect(res).toEqual([
'The rain in ',
'<mark>Spain</mark>',
' falls mainly on the plain. Spain is nice.'
])
})

test('regexp pattern & html string interpolation function with matches', () => {
const str = 'The rain in Spain falls mainly on the plain. Spain is nice.'
const pattern = new RegExp('Spain', 'igm')
const replacer = (match: string): string => `<mark>${match}</mark>`
const res = flexibleStringReplace(pattern, replacer, str)

expect(res).toEqual([
'The rain in ',
'<mark>Spain</mark>',
' falls mainly on the plain. ',
'<mark>Spain</mark>',
' is nice.'
])
})

test('forwards matching group parts in replacer params', () => {
const str = 'Dear @diary, I hope this test #passes #pray'
const pattern = /\s(@\w+)|\s(#\w+)/gm
const ats: string[] = []
const tags: string[] = []
const replacer = (match: string, at: string, tag: string): void => {
if (at) {
ats.push(at)
}
if (tag) {
tags.push(tag)
}
}
flexibleStringReplace(pattern, replacer, str)

expect(ats).toEqual(['@diary'])
expect(tags).toEqual(['#passes', '#pray'])
})

test('regexp pattern & react interpolation function', () => {
const str = 'The rain in Spain falls mainly on the plain. Spain is nice.'
const pattern = new RegExp('Spain', 'igm')
const replacer = (match: string, offset: string): JSX.Element => (
<mark key={offset}>{match}</mark>
)
const res = flexibleStringReplace(pattern, replacer, str)

expect(res).toEqual([
'The rain in ',
<mark key={12}>Spain</mark>,
' falls mainly on the plain. ',
<mark key={45}>Spain</mark>,
' is nice.'
])
})
7 changes: 0 additions & 7 deletions ava.config.js

This file was deleted.

31 changes: 0 additions & 31 deletions index.js

This file was deleted.

10 changes: 10 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
clearMocks: true,
collectCoverage: true,
collectCoverageFrom: ['<rootDir>/source/**/*.{ts,tsx}'],
coveragePathIgnorePatterns: ['/node_modules/', '<rootDir>/source/@types'],
moduleNameMapper: {},
preset: 'ts-jest',
setupFilesAfterEnv: [],
verbose: true
}
2 changes: 1 addition & 1 deletion lint-staged.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
'*.js': ['eslint --fix', 'git add']
'*.{js,ts,tsx}': ['eslint --fix']
}
3 changes: 0 additions & 3 deletions nyc.config.js

This file was deleted.

73 changes: 47 additions & 26 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
{
"name": "@rpearce/flexible-string-replace",
"version": "0.1.3",
"version": "1.0.0",
"description": "🧶 Safely replace any part of a string with anything. Example: useful for replacing substrings with JSX in React",
"main": "index.js",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"repository": {
"type": "git",
"url": "git@github.com:rpearce/flexible-string-replace.git"
},
"homepage": "https://github.com/rpearce/flexible-string-replace",
"bugs": "https://github.com/rpearce/flexible-string-replace/issues",
"author": "Robert Pearce <me@robertwpearce.com>",
"contributors": [
"Robert Pearce <me@robertwpearce.com> (https://robertwpearce.com)"
],
"license": "ISC",
"license": "BSD-3",
"keywords": [
"string-replace",
"string-manipulation",
Expand All @@ -34,32 +32,55 @@
"AUTHORS",
"LICENSE",
"README.md",
"index.js"
"dist/"
],
"sideEffects": false,
"scripts": {
"build": "run-s clean build:js",
"build:js": "rollup -c ./rollup.config.js",
"build:js:watch": "yarn build:js -- -w",
"ci": "run-p lint build test",
"clean": "run-p clean:dist",
"clean:dist": "rm -rf ./dist",
"contributors:add": "all-contributors add",
"contributors:generate": "all-contributors generate",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"coverage:report": "nyc report --reporter=lcov && echo \"view coverage: ./coverage/lcov-report/index.html\"",
"lint": "eslint .",
"prepublishOnly": "run-p lint test",
"test": "nyc ava --verbose"
"coverage": "jest --coverage --coverageReporters=text-lcov | coveralls",
"lint": "eslint . --ext .js,.ts,.tsx",
"prepublishOnly": "run-p lint test && yarn build:js",
"test": "jest"
},
"devDependencies": {
"@babel/preset-react": "^7.7.4",
"all-contributors-cli": "^6.11.2",
"ava": "^2.4.0",
"coveralls": "^3.0.9",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.9.0",
"eslint-plugin-prettier": "^3.1.2",
"eslint-plugin-react": "^7.17.0",
"husky": "^3.1.0",
"lint-staged": "^9.5.0",
"npm-run-all": "^4.1.5",
"nyc": "^15.0.0",
"prettier": "^1.19.1",
"react": "^16.12.0"
"@rollup/plugin-commonjs": "11.0.2",
"@rollup/plugin-node-resolve": "7.1.1",
"@testing-library/jest-dom": "5.1.1",
"@testing-library/react": "10.0.1",
"@types/jest": "25.1.4",
"@types/node": "13.9.1",
"@types/react": "16.9.23",
"@types/react-dom": "16.9.5",
"@typescript-eslint/eslint-plugin": "2.23.0",
"@typescript-eslint/parser": "2.23.0",
"all-contributors-cli": "6.14.0",
"coveralls": "3.0.9",
"eslint": "6.8.0",
"eslint-config-prettier": "6.10.0",
"eslint-plugin-jest": "23.8.2",
"eslint-plugin-jsx-a11y": "6.2.3",
"eslint-plugin-prettier": "3.1.2",
"eslint-plugin-react": "7.19.0",
"husky": "4.2.3",
"jest": "25.1.0",
"lint-staged": "10.0.8",
"npm-run-all": "4.1.5",
"prettier": "1.19.1",
"react": "16.13.0",
"react-dom": "16.13.0",
"rollup": "2.0.6",
"rollup-plugin-postcss": "2.4.1",
"rollup-plugin-terser": "5.3.0",
"rollup-plugin-typescript2": "0.26.0",
"ts-jest": "25.2.1",
"tslib": "1.11.1",
"typescript": "3.8.3"
}
}
Loading

0 comments on commit 90c7f79

Please sign in to comment.