diff --git a/src/methods.js b/src/methods.js index 9b39cb9..67a2d51 100644 --- a/src/methods.js +++ b/src/methods.js @@ -1,6 +1,5 @@ import { - isFunc, - isPlainObject + isFunc } from './utils/tools' import { draw, @@ -104,7 +103,8 @@ export default function methods () { } self.getCropperImage = (opt, fn) => { - const customOptions = opt + const customOptions = Object.assign({fileType: 'jpg'}, opt) + const callback = isFunc(opt) ? opt : isFunc(fn) ? fn : null let canvasOptions = { canvasId: id, @@ -116,10 +116,7 @@ export default function methods () { let task = () => Promise.resolve() - if ( - isPlainObject(customOptions) && - customOptions.original - ) { + if (customOptions.original) { // original mode task = () => { self.targetCtx.drawImage( @@ -144,14 +141,7 @@ export default function methods () { return task() .then(() => { - if (isPlainObject(customOptions)) { - canvasOptions = Object.assign({}, canvasOptions, customOptions) - } - - if (isFunc(customOptions)) { - fn = customOptions - } - + Object.assign(canvasOptions, customOptions) const arg = canvasOptions.componentContext ? [canvasOptions, canvasOptions.componentContext] : [canvasOptions] @@ -160,14 +150,13 @@ export default function methods () { }) .then(res => { const tempFilePath = res.tempFilePath - - return isFunc(fn) - ? fn.call(self, tempFilePath, null) + return callback + ? callback.call(self, tempFilePath, null) : tempFilePath }) .catch((err) => { - if (isFunc(fn)) { - fn.call(self, null, err) + if (callback) { + callback.call(self, null, err) } else { throw err } diff --git a/src/utils/tools.js b/src/utils/tools.js index 8d62ab3..f76ab88 100644 --- a/src/utils/tools.js +++ b/src/utils/tools.js @@ -1,33 +1,33 @@ /** * String type check */ -exports.isStr = (v) => typeof v === 'string' +export const isStr = (v) => typeof v === 'string' /** * Number type check */ -exports.isNum = (v) => typeof v === 'number' +export const isNum = (v) => typeof v === 'number' /** * Array type check */ -exports.isArr = Array.isArray +export const isArr = Array.isArray /** * undefined type check */ -exports.isUndef = (v) => v === undefined +export const isUndef = (v) => v === undefined -exports.isTrue = (v) => v === true +export const isTrue = (v) => v === true -exports.isFalse = (v) => v === false +export const isFalse = (v) => v === false /** * Function type check */ -exports.isFunc = (v) => typeof v === 'function' +export const isFunc = (v) => typeof v === 'function' /** * Quick object check - this is primarily used to tell * Objects from primitive values when we know the value * is a JSON-compliant type. */ -exports.isObj = exports.isObject = (obj) => { +export const isObject = (obj) => { return obj !== null && typeof obj === 'object' } @@ -36,7 +36,7 @@ exports.isObj = exports.isObject = (obj) => { * for plain JavaScript objects. */ const _toString = Object.prototype.toString -exports.isPlainObject = (obj) => { +export const isPlainObject = (obj) => { return _toString.call(obj) === '[object Object]' } @@ -44,7 +44,7 @@ exports.isPlainObject = (obj) => { * Check whether the object has the property. */ const hasOwnProperty = Object.prototype.hasOwnProperty -exports.hasOwn = (obj, key) => { +export const hasOwn = (obj, key) => { return hasOwnProperty.call(obj, key) } @@ -53,12 +53,12 @@ exports.hasOwn = (obj, key) => { * Stubbing args to make Flow happy without leaving useless transpiled code * with ...rest (https://flow.org/blog/2017/05/07/Strict-Function-Call-Arity/) */ -exports.noop = (a, b, c) => {} +export const noop = (a, b, c) => {} /** * Check if val is a valid array index. */ -exports.isValidArrayIndex = (val) => { +export const isValidArrayIndex = (val) => { const n = parseFloat(String(val)) return n >= 0 && Math.floor(n) === n && isFinite(val) }