Commit ba414655 authored by rockyl's avatar rockyl

兼容数组的配置

parent e293595f
/**
* Created by rockyl on 2020-01-27.
*/
var genericRegexp = /(\w+)(<(\w+)>)?/; var genericRegexp = /(\w+)(<(\w+)>)?/;
function compute(props, options) { function compute(props, options) {
var result = props || {}; var result = props || {};
var _loop_1 = function (key) { if (Array.isArray(options)) {
var sourceValue = getValue(result, options, key); for (var _i = 0, options_1 = options; _i < options_1.length; _i++) {
var value = sourceValue; var option = options_1[_i];
if (options) { var key = option.name;
var value = getValue(result, option, key);
result[key] = transform(key, value, option);
}
}
else {
for (var key in options) {
var option = options[key]; var option = options[key];
if (option && option.type) { var value = getValue(result, option, key);
var _a = parseType(option.type), type = _a.type, generic_1 = _a.generic; result[key] = transform(key, value, option);
switch (type) { }
case 'vector2': }
value = parseVector2(sourceValue); return result;
break; function transform(key, value, option) {
case 'array': var sourceValue = value;
var seps = sourceValue.split(','); if (option && option.type) {
seps = seps.map(function (sep) { var _a = parseType(option.type), type = _a.type, generic_1 = _a.generic;
var item; switch (type) {
if (generic_1) { case 'vector2':
switch (generic_1) { value = parseVector2(sourceValue);
case 'number': break;
item = parseFloat(sep); case 'array':
break; var seps = sourceValue.split(',');
case 'boolean': seps = seps.map(function (sep) {
item = sep === 'true'; var item;
break; if (generic_1) {
default: switch (generic_1) {
item = sep; case 'number':
break; item = parseFloat(sep);
} break;
} case 'boolean':
else { item = sep === 'true';
item = sep; break;
default:
item = sep;
break;
} }
return item; }
}); else {
value = seps; item = sep;
break; }
} return item;
});
value = seps;
break;
} }
} }
result[key] = value; return value;
};
for (var key in options) {
_loop_1(key);
} }
return result;
} }
function parseType(typeStr) { function parseType(typeStr) {
var type = typeStr, generic; var type = typeStr, generic;
...@@ -80,13 +85,13 @@ function parseVector2(sourceValue) { ...@@ -80,13 +85,13 @@ function parseVector2(sourceValue) {
} }
return value; return value;
} }
function getValue(props, options, key) { function getValue(props, option, key) {
var value; var value;
if (props.hasOwnProperty(key)) { if (props.hasOwnProperty(key)) {
value = props[key]; value = props[key];
} }
else if (options && options[key].hasOwnProperty('default')) { else if (option && option.hasOwnProperty('default')) {
value = options[key].default; value = option.default;
} }
return value; return value;
} }
......
This diff is collapsed.
...@@ -2,56 +2,61 @@ ...@@ -2,56 +2,61 @@
Object.defineProperty(exports, '__esModule', { value: true }); Object.defineProperty(exports, '__esModule', { value: true });
/**
* Created by rockyl on 2020-01-27.
*/
var genericRegexp = /(\w+)(<(\w+)>)?/; var genericRegexp = /(\w+)(<(\w+)>)?/;
function compute(props, options) { function compute(props, options) {
var result = props || {}; var result = props || {};
var _loop_1 = function (key) { if (Array.isArray(options)) {
var sourceValue = getValue(result, options, key); for (var _i = 0, options_1 = options; _i < options_1.length; _i++) {
var value = sourceValue; var option = options_1[_i];
if (options) { var key = option.name;
var value = getValue(result, option, key);
result[key] = transform(key, value, option);
}
}
else {
for (var key in options) {
var option = options[key]; var option = options[key];
if (option && option.type) { var value = getValue(result, option, key);
var _a = parseType(option.type), type = _a.type, generic_1 = _a.generic; result[key] = transform(key, value, option);
switch (type) { }
case 'vector2': }
value = parseVector2(sourceValue); return result;
break; function transform(key, value, option) {
case 'array': var sourceValue = value;
var seps = sourceValue.split(','); if (option && option.type) {
seps = seps.map(function (sep) { var _a = parseType(option.type), type = _a.type, generic_1 = _a.generic;
var item; switch (type) {
if (generic_1) { case 'vector2':
switch (generic_1) { value = parseVector2(sourceValue);
case 'number': break;
item = parseFloat(sep); case 'array':
break; var seps = sourceValue.split(',');
case 'boolean': seps = seps.map(function (sep) {
item = sep === 'true'; var item;
break; if (generic_1) {
default: switch (generic_1) {
item = sep; case 'number':
break; item = parseFloat(sep);
} break;
} case 'boolean':
else { item = sep === 'true';
item = sep; break;
default:
item = sep;
break;
} }
return item; }
}); else {
value = seps; item = sep;
break; }
} return item;
});
value = seps;
break;
} }
} }
result[key] = value; return value;
};
for (var key in options) {
_loop_1(key);
} }
return result;
} }
function parseType(typeStr) { function parseType(typeStr) {
var type = typeStr, generic; var type = typeStr, generic;
...@@ -84,13 +89,13 @@ function parseVector2(sourceValue) { ...@@ -84,13 +89,13 @@ function parseVector2(sourceValue) {
} }
return value; return value;
} }
function getValue(props, options, key) { function getValue(props, option, key) {
var value; var value;
if (props.hasOwnProperty(key)) { if (props.hasOwnProperty(key)) {
value = props[key]; value = props[key];
} }
else if (options && options[key].hasOwnProperty('default')) { else if (option && option.hasOwnProperty('default')) {
value = options[key].default; value = option.default;
} }
return value; return value;
} }
......
This diff is collapsed.
(function (global, factory) { (function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) : typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global['props-compute'] = {})); (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global['props-compute'] = {}));
}(this, function (exports) { 'use strict'; }(this, (function (exports) { 'use strict';
/**
* Created by rockyl on 2020-01-27.
*/
var genericRegexp = /(\w+)(<(\w+)>)?/; var genericRegexp = /(\w+)(<(\w+)>)?/;
function compute(props, options) { function compute(props, options) {
var result = props || {}; var result = props || {};
var _loop_1 = function (key) { if (Array.isArray(options)) {
var sourceValue = getValue(result, options, key); for (var _i = 0, options_1 = options; _i < options_1.length; _i++) {
var value = sourceValue; var option = options_1[_i];
if (options) { var key = option.name;
var value = getValue(result, option, key);
result[key] = transform(key, value, option);
}
}
else {
for (var key in options) {
var option = options[key]; var option = options[key];
if (option && option.type) { var value = getValue(result, option, key);
var _a = parseType(option.type), type = _a.type, generic_1 = _a.generic; result[key] = transform(key, value, option);
switch (type) { }
case 'vector2': }
value = parseVector2(sourceValue); return result;
break; function transform(key, value, option) {
case 'array': var sourceValue = value;
var seps = sourceValue.split(','); if (option && option.type) {
seps = seps.map(function (sep) { var _a = parseType(option.type), type = _a.type, generic_1 = _a.generic;
var item; switch (type) {
if (generic_1) { case 'vector2':
switch (generic_1) { value = parseVector2(sourceValue);
case 'number': break;
item = parseFloat(sep); case 'array':
break; var seps = sourceValue.split(',');
case 'boolean': seps = seps.map(function (sep) {
item = sep === 'true'; var item;
break; if (generic_1) {
default: switch (generic_1) {
item = sep; case 'number':
break; item = parseFloat(sep);
} break;
} case 'boolean':
else { item = sep === 'true';
item = sep; break;
default:
item = sep;
break;
} }
return item; }
}); else {
value = seps; item = sep;
break; }
} return item;
});
value = seps;
break;
} }
} }
result[key] = value; return value;
};
for (var key in options) {
_loop_1(key);
} }
return result;
} }
function parseType(typeStr) { function parseType(typeStr) {
var type = typeStr, generic; var type = typeStr, generic;
...@@ -86,13 +91,13 @@ ...@@ -86,13 +91,13 @@
} }
return value; return value;
} }
function getValue(props, options, key) { function getValue(props, option, key) {
var value; var value;
if (props.hasOwnProperty(key)) { if (props.hasOwnProperty(key)) {
value = props[key]; value = props[key];
} }
else if (options && options[key].hasOwnProperty('default')) { else if (option && option.hasOwnProperty('default')) {
value = options[key].default; value = option.default;
} }
return value; return value;
} }
...@@ -103,5 +108,5 @@ ...@@ -103,5 +108,5 @@
Object.defineProperty(exports, '__esModule', { value: true }); Object.defineProperty(exports, '__esModule', { value: true });
})); })));
//# sourceMappingURL=index.umd.js.map //# sourceMappingURL=index.umd.js.map
This diff is collapsed.
...@@ -3,14 +3,17 @@ ...@@ -3,14 +3,17 @@
"version": "1.0.0", "version": "1.0.0",
"main": "dist/index.js", "main": "dist/index.js",
"module": "dist/index.es.js", "module": "dist/index.es.js",
"types": "types.d.ts",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"dev": "rollup -c -w", "dev": "rollup -c -w",
"build": "rollup -c" "build": "rollup -c",
"dts": "dts-bundle-generator -o types.d.ts src/index.ts"
}, },
"devDependencies": { "devDependencies": {
"dts-bundle-generator": "^5.4.0",
"rollup-plugin-typescript": "^1.0.1", "rollup-plugin-typescript": "^1.0.1",
"tslib": "^1.11.1", "tslib": "^2.0.1",
"typescript": "^3.8.3" "typescript": "^4.0.3"
} }
} }
...@@ -2,9 +2,8 @@ ...@@ -2,9 +2,8 @@
<module type="WEB_MODULE" version="4"> <module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true"> <component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output /> <exclude-output />
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$" />
<excludeFolder url="file://$MODULE_DIR$/dist" /> <orderEntry type="inheritedJdk" />
</content>
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
</module> </module>
\ No newline at end of file
"use strict";
/**
* Created by rockyl on 2020-01-27.
*/
Object.defineProperty(exports, "__esModule", { value: true });
var genericRegexp = /(\w+)(<(\w+)>)?/;
function compute(props, options) {
var result = props || {};
var _loop_1 = function (key) {
var sourceValue = getValue(result, options, key);
var value = sourceValue;
if (options) {
var option = options[key];
if (option && option.type) {
var _a = parseType(option.type), type = _a.type, generic_1 = _a.generic;
switch (type) {
case 'vector2':
value = parseVector2(sourceValue);
break;
case 'array':
var seps = sourceValue.split(',');
seps = seps.map(function (sep) {
var item;
if (generic_1) {
switch (generic_1) {
case 'number':
item = parseFloat(sep);
break;
case 'boolean':
item = sep === 'true';
break;
default:
item = sep;
break;
}
}
else {
item = sep;
}
return item;
});
value = seps;
break;
}
}
}
result[key] = value;
};
for (var key in options) {
_loop_1(key);
}
return result;
}
exports.compute = compute;
function parseType(typeStr) {
var type = typeStr, generic;
var regResult = typeStr.match(genericRegexp);
if (regResult) {
type = regResult[1];
generic = regResult[3];
}
return {
type: type, generic: generic,
};
}
exports.parseType = parseType;
function parseVector2(sourceValue) {
var value = sourceValue;
if (!sourceValue) {
value = { x: undefined, y: undefined };
}
if (typeof sourceValue === 'string') {
var arr = sourceValue.split(',');
value = {
x: arr[0] === '' ? undefined : parseFloat(arr[0]),
y: arr[1] === '' ? undefined : parseFloat(arr[1]),
};
}
else if (Array.isArray(sourceValue)) {
value = {
x: sourceValue[0] === '' ? undefined : parseFloat(sourceValue[0]),
y: sourceValue[1] === '' ? undefined : parseFloat(sourceValue[1]),
};
}
return value;
}
exports.parseVector2 = parseVector2;
function getValue(props, options, key) {
var value;
if (props.hasOwnProperty(key)) {
value = props[key];
}
else if (options && options[key].hasOwnProperty('default')) {
value = options[key].default;
}
return value;
}
...@@ -4,48 +4,60 @@ ...@@ -4,48 +4,60 @@
const genericRegexp = /(\w+)(<(\w+)>)?/; const genericRegexp = /(\w+)(<(\w+)>)?/;
export function compute(props, options) { export function compute(props, options: Object | Array<any>) {
let result = props || {}; let result = props || {};
for (let key in options) { if (Array.isArray(options)) {
let sourceValue = getValue(result, options, key); for (let option of options) {
let value = sourceValue; let key = option.name;
if (options) { let value = getValue(result, option, key);
result[key] = transform(key, value, option);
}
} else {
for (let key in options) {
let option = options[key]; let option = options[key];
if (option && option.type) { let value = getValue(result, option, key);
let {type, generic} = parseType(option.type);
switch (type) { result[key] = transform(key, value, option);
case 'vector2': }
value = parseVector2(sourceValue); }
break; return result;
case 'array':
let seps = sourceValue.split(','); function transform(key, value, option) {
seps = seps.map(sep => { let sourceValue = value;
let item; if (option && option.type) {
if (generic) { let {type, generic} = parseType(option.type);
switch (generic) { switch (type) {
case 'number': case 'vector2':
item = parseFloat(sep); value = parseVector2(sourceValue);
break; break;
case 'boolean': case 'array':
item = sep === 'true'; let seps = sourceValue.split(',');
break; seps = seps.map(sep => {
default: let item;
item = sep; if (generic) {
break; switch (generic) {
} case 'number':
} else { item = parseFloat(sep);
item = sep; break;
case 'boolean':
item = sep === 'true';
break;
default:
item = sep;
break;
} }
return item; } else {
}); item = sep;
value = seps; }
break; return item;
} });
value = seps;
break;
} }
} }
result[key] = value; return value;
} }
return result;
} }
export function parseType(typeStr) { export function parseType(typeStr) {
...@@ -81,12 +93,12 @@ export function parseVector2(sourceValue) { ...@@ -81,12 +93,12 @@ export function parseVector2(sourceValue) {
return value; return value;
} }
function getValue(props, options, key) { function getValue(props, option, key) {
let value; let value;
if (props.hasOwnProperty(key)) { if (props.hasOwnProperty(key)) {
value = props[key]; value = props[key];
} else if (options && options[key].hasOwnProperty('default')) { } else if (option && option.hasOwnProperty('default')) {
value = options[key].default; value = option.default;
} }
return value; return value;
......
{
"compilerOptions": {
"module": "ES2015",
"target": "es5",
"noImplicitAny": false,
"sourceMap": true,
"removeComments": true,
"noEmitOnError": true,
"noEmitHelpers": true,
"experimentalDecorators": true,
"outDir": "dist-m",
"lib": [
"es5",
"dom",
"es2015.promise"
]
},
"include": [
"src"
],
"exclude": [
"node_modules"
]
}
\ No newline at end of file
// Generated by dts-bundle-generator v5.4.0
export declare function compute(props: any, options: Object | Array<any>): any;
export declare function parseType(typeStr: any): {
type: any;
generic: any;
};
export declare function parseVector2(sourceValue: any): any;
export {};
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment