Skip to content

Commit

Permalink
updated with bug fixes and new features
Browse files Browse the repository at this point in the history
  • Loading branch information
dhershman1 committed Feb 24, 2020
1 parent cbb9432 commit 4338d9b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 16 deletions.
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.

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

0 comments on commit 4338d9b

Please sign in to comment.