Skip to content

Commit

Permalink
Merge pull request #25 from dhershman1/development
Browse files Browse the repository at this point in the history
Development v2.5.0
  • Loading branch information
dhershman1 authored Feb 24, 2020
2 parents 5268e7f + 4338d9b commit 873bb62
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 69 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# Test HTML

tests/test.html

# Stupid Mac Files

.DS_Store

# Logs
logs
*.log
Expand Down
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# Change Log

## v2.5.0

### New

- Added `fireOnEmpty` modifier, which allow you to choose individual inputs you want to have fireOnEmpty
- Added `cancelOnEmpty` modifier which cancels the debounce all together if the input value is empty

### Improved

- Drastic code cleanup

### Fixed

- Bug with `fireOnEmpty` where debounce function would fire twice even when input was empty

## v2.4.0

### New
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ npm i vue-debounce
- `lock` : Used to lock the debounce and prevent the enter key from triggering the function when pressed
- Example: `v-debounce:400ms.lock="cb"`
- `unlock` : Used to unlock the enter key on a debounced input, useful if you want to use the `lock` option and only want a few debounced inputs unlocked
- `fireOnEmpty` : Use to signify that when that specific input is emptied, you want the function to fire right away
- `cancelOnEmpty` : Use this to specify that when the input is emptied you **DO NOT** want your debounced function to trigger at all

## Options

Expand Down
2 changes: 1 addition & 1 deletion dist/vue-debounce.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-debounce",
"version": "2.4.0",
"version": "2.5.0",
"description": "A simple vue directive for debounce",
"main": "dist/vue-debounce.min.js",
"types": "types/index.d.ts",
Expand Down Expand Up @@ -39,7 +39,7 @@
"devDependencies": {
"chokidar": "3.3.1",
"esm": "3.2.25",
"rollup": "1.29.0",
"rollup": "1.31.1",
"rollup-plugin-buble": "0.19.8",
"rollup-plugin-uglify": "6.0.4",
"standard": "14.3.1",
Expand Down
79 changes: 64 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,87 @@
import debounce from './debounce'

// Helper Functions
/**
* Maps through an array of strings and lowercases all of them
* @param {Array} list an array of strings to map through
*/
function toLowerMap (list) {
return list.map(x => x.toLowerCase())
}

/**
* Takes in a value and ensures its wrapped within an array
* @param {Any} value The value to ensure is an array
*/
function ensureArray (value) {
if (Array.isArray(value)) {
return value
}

if (value == null) {
return []
}

return [value]
}

// Figures out the event we are using with the bound element
function figureOutEvent (attrs, listenTo) {
function mapOutListeningEvents (attrs, listenTo) {
const { value = false } = attrs.getNamedItem('debounce-events') || {}
const toLowerMap = list => list.map(x => x.toLowerCase())

// If they set an events attribute that overwrites everything
if (value) {
return toLowerMap(value.split(','))
}

return Array.isArray(listenTo) ? toLowerMap(listenTo) : [listenTo]
return toLowerMap(ensureArray(listenTo))
}

function isEmpty (str) {
return str === ''
}

function isCanceled (inputValue, modifiers) {
return isEmpty(inputValue) && modifiers.cancelonempty
}

function isLocked (key, modifiers) {
return key === 'Enter' && (!modifiers.lock || modifiers.unlock)
}

function shouldFireOnEmpty (inputValue, modifiers) {
return isEmpty(inputValue) && modifiers.fireonempty
}

export { debounce }

export default {
install (Vue, { lock, listenTo = 'keyup', defaultTime = '300ms', fireOnEmpty = false } = {}) {
install (Vue, {
lock = false,
listenTo = 'keyup',
defaultTime = '300ms',
fireOnEmpty = false,
cancelOnEmpty = false
} = {}) {
Vue.directive('debounce', {
bind (el, { value, arg, modifiers }) {
const listener = figureOutEvent(el.attributes, listenTo)
bind (el, {
value: debouncedFn,
arg: timer = defaultTime,
modifiers
}) {
const combinedRules = Object.assign({ fireonempty: fireOnEmpty, cancelonempty: cancelOnEmpty, lock }, modifiers)
const listener = mapOutListeningEvents(el.attributes, listenTo)
const fn = debounce(e => {
value(e.target.value, e)
}, arg || defaultTime)
debouncedFn(e.target.value, e)
}, timer)

function handler (event) {
const isUnlocked = (!modifiers.lock && !lock) || modifiers.unlock

if ((event.key === 'Enter' && isUnlocked) || (!event.target.value && fireOnEmpty)) {
if (isCanceled(event.target.value, combinedRules)) {
fn.cancel()
value(event.target.value, event)
}

if (event.key !== 'Enter') {
} else if (isLocked(event.key, combinedRules) || shouldFireOnEmpty(event.target.value, combinedRules)) {
fn.cancel()
debouncedFn(event.target.value, event)
} else {
fn(event)
}
}
Expand Down
51 changes: 0 additions & 51 deletions tests/test.html

This file was deleted.

1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface PluginConfig {
lock?: boolean
listenTo?: string | string[]
defaultTime?: string | number
fireOnEmpty?: boolean
}

export interface PluginObject {
Expand Down

0 comments on commit 873bb62

Please sign in to comment.