@wojtekmaj/predict-input-value
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -0,1 +1,12 @@ | ||
var excludeList = [ | ||
'Alt', | ||
'ArrowDown', | ||
'ArrowLeft', | ||
'ArrowRight', | ||
'ArrowUp', | ||
'Enter', | ||
'Escape', | ||
'Shift', | ||
'Tab', | ||
]; | ||
/** | ||
@@ -8,46 +19,48 @@ * Predicts what the value will be after the next keyup given keydown event. | ||
export default function predictInputValue(event) { | ||
// Support only keydown and keypress event | ||
if (event.type !== 'keydown' && event.type !== 'keypress') { | ||
return null; | ||
} // Skip Cmd+A and other key combinations | ||
if (event.metaKey) { | ||
return null; | ||
} | ||
var element = event.target; // We can’t predict values in number inputs | ||
if (element.type === 'number') { | ||
return null; | ||
} | ||
var selectionStart = element.selectionStart; | ||
var selectionEnd = element.selectionEnd; | ||
var nextValueArr = element.value.split(''); | ||
var replaceWith = event.key; | ||
if (event.key === 'Backspace') { | ||
if (selectionStart && selectionStart === selectionEnd) { | ||
/** | ||
* There’s no text selected, so pressing backspace will remove the character before the caret. | ||
* That’s equal to one character before the caret being selected when Backspace is pressed. | ||
*/ | ||
selectionStart -= 1; | ||
// Support only keydown and keypress event | ||
if (event.type !== 'keydown' && event.type !== 'keypress') { | ||
return null; | ||
} | ||
replaceWith = ''; | ||
} | ||
/** | ||
* If we’re going to add another character, check if we’re not going over the limit set by | ||
* maxLength. If so, entering the next character will fail, and thus, nextValue will be equal to | ||
* value. | ||
*/ | ||
if (!replaceWith || element.maxLength < 0 || nextValueArr.length < element.maxLength) { | ||
nextValueArr.splice(selectionStart, selectionEnd - selectionStart, replaceWith); | ||
} | ||
return nextValueArr.join(''); | ||
} | ||
// Skip Cmd+A and other key combinations | ||
if (event.metaKey) { | ||
return null; | ||
} | ||
if (excludeList.includes(event.key)) { | ||
return null; | ||
} | ||
var element = event.target; | ||
// Only support input and textarea elements | ||
if (!(element instanceof HTMLInputElement) && !(element instanceof HTMLTextAreaElement)) { | ||
return null; | ||
} | ||
// We can’t predict values in number inputs | ||
if (element.type === 'number') { | ||
return null; | ||
} | ||
var selectionStart = element.selectionStart; | ||
var selectionEnd = element.selectionEnd; | ||
if (selectionStart === null || selectionEnd === null) { | ||
return null; | ||
} | ||
var nextValueArr = element.value.split(''); | ||
var replaceWith = event.key; | ||
if (event.key === 'Backspace') { | ||
if (selectionStart && selectionStart === selectionEnd) { | ||
/** | ||
* There’s no text selected, so pressing backspace will remove the character before the caret. | ||
* That’s equal to one character before the caret being selected when Backspace is pressed. | ||
*/ | ||
selectionStart -= 1; | ||
} | ||
replaceWith = ''; | ||
} | ||
/** | ||
* If we’re going to add another character, check if we’re not going over the limit set by | ||
* maxLength. If so, entering the next character will fail, and thus, nextValue will be equal to | ||
* value. | ||
*/ | ||
if (!replaceWith || element.maxLength < 0 || nextValueArr.length < element.maxLength) { | ||
nextValueArr.splice(selectionStart, selectionEnd - selectionStart, replaceWith); | ||
} | ||
return nextValueArr.join(''); | ||
} |
{ | ||
"name": "@wojtekmaj/predict-input-value", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Predicts the input value during KeyDown or KeyPress events, before the input is actually changed", | ||
"main": "dist/umd/index.js", | ||
"main": "dist/cjs/index.js", | ||
"module": "dist/esm/index.js", | ||
"source": "src/index.js", | ||
"source": "src/index.ts", | ||
"types": "src/index.ts", | ||
"sideEffects": false, | ||
"scripts": { | ||
"build": "yarn build-esm && yarn build-umd", | ||
"build-esm": "cross-env BABEL_ENV=production-esm babel src -d dist/esm --ignore **/*.spec.js", | ||
"build-umd": "cross-env BABEL_ENV=production-umd babel src -d dist/umd --ignore **/*.spec.js", | ||
"build": "yarn build-esm && yarn build-cjs", | ||
"build-esm": "tsc --project tsconfig.build.json --outDir dist/esm --module esnext", | ||
"build-cjs": "tsc --project tsconfig.build.json --outDir dist/cjs --module commonjs", | ||
"clean": "rimraf dist", | ||
"jest": "jest", | ||
"lint": "eslint src/ --ext .jsx,.js", | ||
"prepublishOnly": "yarn clean && yarn build", | ||
"test": "yarn lint && yarn jest" | ||
"lint": "eslint src", | ||
"prepack": "yarn clean && yarn build", | ||
"prettier": "prettier --check . --cache", | ||
"test": "yarn lint && yarn tsc && yarn prettier && yarn jest", | ||
"tsc": "tsc --noEmit" | ||
}, | ||
@@ -29,18 +32,34 @@ "keywords": [ | ||
"devDependencies": { | ||
"@babel/cli": "^7.4.0", | ||
"@babel/core": "^7.4.0", | ||
"@babel/preset-env": "^7.4.0", | ||
"cross-env": "^7.0.0", | ||
"eslint": "^6.8.0", | ||
"eslint-config-airbnb-base": "^14.1.0", | ||
"eslint-plugin-import": "^2.20.0", | ||
"jest": "^26.0.0" | ||
"@babel/core": "^7.15.0", | ||
"@babel/preset-env": "^7.15.0", | ||
"@babel/preset-typescript": "^7.18.6", | ||
"@types/jest": "^29.0.0", | ||
"@typescript-eslint/eslint-plugin": "^5.41.0", | ||
"@typescript-eslint/parser": "^5.44.0", | ||
"eslint": "^8.26.0", | ||
"eslint-config-wojtekmaj": "^0.7.1", | ||
"husky": "^8.0.0", | ||
"jest": "^29.0.0", | ||
"jest-environment-jsdom": "^29.0.0", | ||
"prettier": "^2.7.0", | ||
"pretty-quick": "^3.1.0", | ||
"rimraf": "^3.0.0", | ||
"typescript": "^4.9.4" | ||
}, | ||
"resolutions": { | ||
"semver@7.0.0": "^7.0.0" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"files": [ | ||
"LICENSE", | ||
"README.md", | ||
"dist/", | ||
"src/" | ||
"dist", | ||
"src" | ||
], | ||
"repository": "https://github.com/wojtekmaj/predict-input-value.git" | ||
} | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/wojtekmaj/predict-input-value.git" | ||
}, | ||
"funding": "https://github.com/wojtekmaj/predict-input-value?sponsor=1", | ||
"packageManager": "yarn@3.1.0" | ||
} |
@@ -1,11 +0,13 @@ | ||
[![npm](https://img.shields.io/npm/v/predict-input-value.svg)](https://www.npmjs.com/package/predict-input-value) ![downloads](https://img.shields.io/npm/dt/predict-input-value.svg) [![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg)](https://github.com/facebook/jest) | ||
[![npm](https://img.shields.io/npm/v/@wojtekmaj/predict-input-value.svg)](https://www.npmjs.com/package/@wojtekmaj/predict-input-value) ![downloads](https://img.shields.io/npm/dt/@wojtekmaj/predict-input-value.svg) [![CI](https://github.com/wojtekmaj/predict-input-value/workflows/CI/badge.svg)](https://github.com/wojtekmaj/predict-input-value/actions) [![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg)](https://github.com/facebook/jest) | ||
# predict-input-value | ||
# Predict-Input-Value | ||
Predicts the input value during KeyDown or KeyPress events, before the input is actually changed. | ||
## tl;dr | ||
* Install by executing `npm install @wojtekmaj/predict-input-value` or `yarn add @wojtekmaj/predict-input-value`. | ||
* Import by adding `import predictInputValue from '@wojtekmaj/predict-input-value'`. | ||
* Use it by writing `const nextValue = predictInputValue(event)`. | ||
- Install by executing `npm install @wojtekmaj/predict-input-value` or `yarn add @wojtekmaj/predict-input-value`. | ||
- Import by adding `import predictInputValue from '@wojtekmaj/predict-input-value'`. | ||
- Use it by writing `const nextValue = predictInputValue(event)`. | ||
## Example | ||
@@ -15,5 +17,5 @@ | ||
* Input: value `"hello"` | ||
* Selected text: `"ello"` | ||
* Key pressed: `i` | ||
- Input: value `"hello"` | ||
- Selected text: `"ello"` | ||
- Key pressed: `i` | ||
@@ -26,3 +28,2 @@ ```js | ||
## License | ||
@@ -42,5 +43,5 @@ | ||
<a href="mailto:kontakt@wojtekmaj.pl">kontakt@wojtekmaj.pl</a><br /> | ||
<a href="http://wojtekmaj.pl">http://wojtekmaj.pl</a> | ||
<a href="https://wojtekmaj.pl">https://wojtekmaj.pl</a> | ||
</td> | ||
</tr> | ||
</table> |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
15668
9
321
0
45
0
15
1