-
Notifications
You must be signed in to change notification settings - Fork 1
/
modelz.min.js.map
1 lines (1 loc) · 13 KB
/
modelz.min.js.map
1
{"version":3,"file":"modelz.min.js","sources":["src/util.js","src/index.js"],"sourcesContent":["'use strict'\n\nexport function noop() {}\n\nexport function identity(thing) {\n return thing\n}\n\nexport function type(obj) {\n return {}.toString.call(obj)\n}\n\nexport function isObject(thing) {\n return thing !== null && type(thing) === '[object Object]'\n}\n\nexport function isArray(thing) {\n return type(thing) == '[object Array]'\n}\n\nexport function isFunction(thing) {\n return typeof thing === 'function'\n}\n\nexport function isString(thing) {\n return typeof thing === 'string'\n}\n\nexport function isNumber(thing) {\n return typeof thing === 'number'\n}\n\nexport function isUndefined(thing) {\n return typeof thing === 'undefined'\n}\n","'use strict'\n\nimport {\n identity,\n isArray,\n isFunction,\n isNumber,\n isObject,\n isString,\n noop,\n} from './util.js'\n\nconst defaultFieldConfig = {\n construct: identity,\n getCacheKey: noop,\n enumerable: true,\n required: false,\n}\n\nconst defaultGlobalConfig = {\n castString: true,\n parseNumbers: true,\n onChangeListener: () => noop,\n extraProperties: false,\n embedPlainData: true,\n preInit: identity,\n postInit: identity,\n types: {},\n}\n\nfunction createCacheFunction({ cacheKey }) {\n if (isArray(cacheKey)) {\n return (obj) =>\n cacheKey\n .map((prop) =>\n obj[prop] instanceof Date ? obj[prop].getTime() : obj[prop]\n )\n .join('|<3|')\n }\n if (isFunction(cacheKey)) {\n return cacheKey\n }\n return noop\n}\n\nfunction modelz(globalConfig) {\n const {\n castString,\n debug,\n embedPlainData,\n extraProperties,\n onChangeListener,\n parseNumbers,\n postInit,\n preInit,\n types,\n } = {\n ...defaultGlobalConfig,\n ...globalConfig,\n }\n function getConstructor(item, fieldName) {\n const constructors = {\n string(value) {\n if (isString(value)) {\n return value\n }\n if (castString) {\n return '' + value\n }\n throw Error(`Expect a string for \"${fieldName}\", got \"${value}\"`)\n },\n number(value) {\n if (isNumber(value)) {\n return value\n }\n if (isString(value) && parseNumbers) {\n return parseFloat(value)\n }\n throw Error(`Expect a number for \"${fieldName}\", got \"${value}\"`)\n },\n boolean(value) {\n return !!value\n },\n array(value) {\n return [...value]\n },\n object(value) {\n return { ...value }\n },\n date(value) {\n return new Date(value)\n },\n identity,\n ...types,\n }\n\n if (isFunction(item)) {\n return item\n }\n if (constructors[item] == null) {\n throw Error(\n `Try to use unknown type \"${item}\" as type for \"${fieldName}\"`\n )\n }\n return constructors[item]\n }\n function parseConfig(fieldConfig, fieldName) {\n if (isObject(fieldConfig)) {\n if (isFunction(fieldConfig.construct)) {\n // constructor\n return fieldConfig\n }\n\n if (isString(fieldConfig.type)) {\n // type\n return {\n construct: getConstructor(fieldConfig.type, fieldName),\n ...fieldConfig,\n }\n }\n\n if (isFunction(fieldConfig.get)) {\n return {\n getCacheKey: createCacheFunction(fieldConfig),\n get: fieldConfig.get,\n set: fieldConfig.set,\n enumerable: fieldConfig.enumerable || false,\n }\n }\n }\n\n if (isArray(fieldConfig) && fieldConfig.length === 2) {\n // short syntax without default [type, required]\n const [type, required] = fieldConfig\n return {\n construct: getConstructor(type, fieldName),\n required,\n }\n }\n\n if (isArray(fieldConfig) && fieldConfig.length === 3) {\n // short syntax [type, required, default]\n const [type, required, defaultValue] = fieldConfig\n return {\n construct: getConstructor(type, fieldName),\n required,\n default: defaultValue,\n }\n }\n\n if (isFunction(fieldConfig)) {\n // plain construct\n return {\n construct: fieldConfig,\n }\n }\n\n if (isString(fieldConfig)) {\n try {\n // init by type\n return {\n construct: getConstructor(fieldConfig, fieldName),\n }\n } catch (e) {\n // fail silently and try next init\n }\n }\n\n try {\n // init by default\n return {\n construct: getConstructor(typeof fieldConfig, fieldName),\n required: true,\n default: fieldConfig,\n }\n } catch (e) {\n throw new Error(\n `No proper config handler found for config:\\n${JSON.stringify(\n fieldConfig\n )}`\n )\n }\n }\n\n return function Schema(fields, config) {\n config = {\n embedPlainData,\n extraProperties,\n preInit,\n postInit,\n onChangeListener,\n ...config,\n }\n const modelName = config.name ? config.name : 'instance'\n const thisSchema = { modelName }\n return function construct(sourceData = {}) {\n debug &&\n debug.extend('create')(\n `constructing ${modelName} with data %O`,\n sourceData\n )\n if (sourceData._schema === thisSchema) {\n return sourceData\n }\n const _data = {}\n let onChange = noop\n\n let result = {}\n if (config.extraProperties) {\n result = { ...sourceData }\n } else if (debug) {\n const fieldKeys = Object.keys(fields)\n const ignoredProperties = Object.keys(sourceData).filter(\n (key) => !fieldKeys.includes(key)\n )\n if (ignoredProperties.length) {\n debug.extend('warn')(\n `The properties %o are not defined on target ${modelName} and will therefore be dropped.`,\n ignoredProperties\n )\n }\n }\n\n if (config.embedPlainData) {\n Object.defineProperty(result, '_data', {\n get: () => _data,\n enumerable: false,\n })\n }\n Object.defineProperty(result, '_isInitialized', {\n get: () => true,\n enumerable: false,\n })\n Object.defineProperty(result, '_schema', {\n get: () => thisSchema,\n enumerable: false,\n })\n result = config.preInit(result)\n onChange = config.onChangeListener(result)\n for (const fieldName in fields) {\n const fieldConfig = {\n ...defaultFieldConfig,\n ...parseConfig(fields[fieldName], fieldName),\n }\n Object.defineProperty(result, fieldName, {\n enumerable: fieldConfig.enumerable,\n get: function () {\n if (fieldConfig.get) {\n const key = fieldConfig.getCacheKey(result)\n if (\n !_data.hasOwnProperty(fieldName) ||\n key == null ||\n key !== _data[fieldName].key\n ) {\n _data[fieldName] = { key, value: fieldConfig.get(result) }\n }\n return _data[fieldName].value\n }\n return result._data[fieldName]\n },\n set: function (value) {\n const oldValue = result[fieldName]\n if (isFunction(fieldConfig.set)) {\n fieldConfig.set(result, value)\n } else if (!fieldConfig.required && value == null) {\n _data[fieldName] = value = null\n } else {\n _data[fieldName] = fieldConfig.construct(\n value,\n result,\n fieldConfig\n )\n }\n onChange(fieldName, value, oldValue)\n },\n })\n\n if (sourceData[fieldName] != null) {\n result[fieldName] = sourceData[fieldName]\n } else if (fieldConfig.hasOwnProperty('default')) {\n if (isFunction(fieldConfig.default)) {\n result[fieldName] = fieldConfig.default(sourceData)\n } else {\n result[fieldName] = fieldConfig.default\n }\n } else if (fieldConfig.required) {\n throw Error('No value set for ' + fieldName)\n } else if (!fieldConfig.get) {\n // default to null if it's not a computed prop\n result[fieldName] = null\n }\n }\n result = config.postInit(result)\n if (!extraProperties) {\n Object.seal(result)\n }\n return result\n }\n }\n}\n\nexport default modelz\n"],"names":["noop","identity","thing","type","obj","toString","call","isArray","isFunction","isString","defaultFieldConfig","construct","getCacheKey","enumerable","required","defaultGlobalConfig","castString","parseNumbers","onChangeListener","extraProperties","embedPlainData","preInit","postInit","types","createCacheFunction","cacheKey","map","prop","Date","getTime","join","globalConfig","debug","getConstructor","item","fieldName","constructors","string","value","Error","number","parseFloat","boolean","array","object","date","parseConfig","fieldConfig","get","set","length","defaultValue","default","e","JSON","stringify","fields","config","modelName","name","thisSchema","sourceData","extend","_schema","_data","onChange","result","fieldKeys","Object","keys","ignoredProperties","filter","key","includes","defineProperty","hasOwnProperty","oldValue","seal"],"mappings":"uOAEO,SAASA,IAAS,CAElB,SAASC,EAASC,GACvB,OAAOA,CACT,CAEO,SAASC,EAAKC,GACnB,MAAO,GAAGC,SAASC,KAAKF,EAC1B,CAMO,SAASG,EAAQL,GACtB,MAAsB,kBAAfC,EAAKD,EACd,CAEO,SAASM,EAAWN,GACzB,MAAwB,mBAAVA,CAChB,CAEO,SAASO,EAASP,GACvB,MAAwB,iBAAVA,CAChB,CCdA,MAAMQ,EAAqB,CACzBC,UAAWV,EACXW,YAAaZ,EACba,YAAY,EACZC,UAAU,GAGNC,EAAsB,CAC1BC,YAAY,EACZC,cAAc,EACdC,iBAAkB,IAAMlB,EACxBmB,iBAAiB,EACjBC,gBAAgB,EAChBC,QAASpB,EACTqB,SAAUrB,EACVsB,MAAO,CAAE,GAGX,SAASC,GAAoBC,SAAEA,IAC7B,OAAIlB,EAAQkB,GACFrB,GACNqB,EACGC,KAAKC,GACJvB,EAAIuB,aAAiBC,KAAOxB,EAAIuB,GAAME,UAAYzB,EAAIuB,KAEvDG,KAAK,QA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