UNPKG

@onesy/utils

Version:
31 lines (25 loc) 743 B
import is from './is'; const optionsDefault = { decode: false, decodeMethod: decodeURIComponent }; const castParam = function (value) { let options_ = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; const options = { ...optionsDefault, ...options_ }; let newValue = value; try { if (is('string', value) && options.decode && is('function', options.decodeMethod)) newValue = options.decodeMethod(value); } catch (error) {} try { if (is('string', newValue)) { if ('undefined' === newValue) return undefined; if ('NaN' === newValue) return NaN; return JSON.parse(newValue); } return newValue; } catch (error) {} return newValue; }; export default castParam;