javascript-helpers
Advanced tools
Comparing version 1.2.1 to 1.3.0
@@ -5,3 +5,3 @@ { | ||
"description": "List of useful JavaScript functions to make development easier", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"main": "src/index.js", | ||
@@ -31,4 +31,4 @@ "homepage": "https://valeriodipunzio.com/plugins/javascript-helpers/", | ||
"devDependencies": { | ||
"@babel/core": "^7.9.0", | ||
"@babel/preset-env": "^7.9.0", | ||
"@babel/core": "^7.11.6", | ||
"@babel/preset-env": "^7.11.5", | ||
"babel-jest": "^24.9.0", | ||
@@ -35,0 +35,0 @@ "jest": "^24.9.0" |
@@ -7,2 +7,5 @@ | ||
export { default as checkFormElement } from './modules/checkFormElement/checkFormElement.js'; | ||
export { default as deepFreeze } from './modules/deepFreeze/deepFreeze.js'; | ||
export { default as emitEvent } from './modules/emitEvent/emitEvent.js'; | ||
export { default as emitCustomEvent } from './modules/emitCustomEvent/emitCustomEvent.js'; | ||
export { default as fillForm } from './modules/fillForm/fillForm.js'; | ||
@@ -9,0 +12,0 @@ export { default as getAge } from './modules/getAge/getAge.js' |
@@ -9,3 +9,22 @@ | ||
export default ( options ) => { | ||
const runFetch = (options, timeoutTimer) => { | ||
return fetch( options.url, options ) | ||
.then(response => { | ||
if( !response.ok ){ | ||
return Promise.reject(response); | ||
} | ||
const fetchMethod = getFetchMethod( response, options ); | ||
return response[fetchMethod](); | ||
}) | ||
.finally(() => { | ||
if( timeoutTimer ){ | ||
window.clearTimeout( timeoutTimer ); | ||
} | ||
}); | ||
}; | ||
export default ( options = {}, canAbortOrController = false ) => { | ||
const canAbort = !!canAbortOrController; | ||
const hasController = canAbortOrController.signal; | ||
const controller = hasController ? canAbortOrController : (options.timeout > 0 || canAbort ? new AbortController() : undefined); | ||
let timeoutTimer; | ||
@@ -17,30 +36,21 @@ | ||
if ( options.timeout > 0 ) { | ||
const controller = new AbortController(); | ||
if( controller ){ | ||
options.signal = controller.signal; | ||
timeoutTimer = window.setTimeout(() => { | ||
controller.abort(); | ||
}, options.timeout); | ||
if( options.timeout > 0 ){ | ||
timeoutTimer = window.setTimeout(() => { | ||
controller.abort(); | ||
}, options.timeout); | ||
} | ||
} | ||
return fetch( options.url, options ) | ||
.then(response => { | ||
if( !response.ok ){ | ||
return Promise.reject(response); | ||
} | ||
const fetchMethod = getFetchMethod( response, options ); | ||
return response[fetchMethod](); | ||
}) | ||
.finally(() => { | ||
if( timeoutTimer ){ | ||
window.clearTimeout( timeoutTimer ); | ||
} | ||
}); | ||
return ( | ||
canAbort ? | ||
{ | ||
abort: () => controller.abort(), | ||
ready: runFetch( options, timeoutTimer ) | ||
} | ||
: | ||
runFetch( options, timeoutTimer ) | ||
) | ||
} |
@@ -14,12 +14,8 @@ | ||
if( fieldEl.type !== 'checkbox' && fieldEl.type !== 'radio' ){ | ||
let containerEl = fieldEl.closest('[data-formjs-question]') || fieldEl; | ||
const containerEl = fieldEl.closest('[data-formjs-question]') || fieldEl; | ||
if( fieldEl.value ){ | ||
if( fieldEl.value ){ | ||
addClass( containerEl, cssClasses ); | ||
} else { | ||
removeClass( containerEl, cssClasses ); | ||
} | ||
@@ -26,0 +22,0 @@ } |
@@ -6,6 +6,6 @@ | ||
Array.from(arguments).filter(arg => !!arg).forEach(arg => { | ||
Array.from(arguments).slice(1).filter(arg => !!arg).forEach(arg => { | ||
Object.keys(arg).forEach(key => { | ||
if ( isPlainObject(arg[key]) ){ | ||
out[key] = mergeObjects(out[key], arg[key]); | ||
out[key] = mergeObjects((out[key] || {}), arg[key]); | ||
} else { | ||
@@ -12,0 +12,0 @@ out[key] = arg[key]; |
32709
50
651