Commit ba414655 authored by rockyl's avatar rockyl

兼容数组的配置

parent e293595f
/**
* Created by rockyl on 2020-01-27.
*/
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) {
if (Array.isArray(options)) {
for (var _i = 0, options_1 = options; _i < options_1.length; _i++) {
var option = options_1[_i];
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];
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;
var value = getValue(result, option, key);
result[key] = transform(key, value, option);
}
}
return result;
function transform(key, value, option) {
var sourceValue = value;
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;
}
return item;
});
value = seps;
break;
}
}
else {
item = sep;
}
return item;
});
value = seps;
break;
}
}
result[key] = value;
};
for (var key in options) {
_loop_1(key);
return value;
}
return result;
}
function parseType(typeStr) {
var type = typeStr, generic;
......@@ -80,13 +85,13 @@ function parseVector2(sourceValue) {
}
return value;
}
function getValue(props, options, key) {
function getValue(props, option, key) {
var value;
if (props.hasOwnProperty(key)) {
value = props[key];
}
else if (options && options[key].hasOwnProperty('default')) {
value = options[key].default;
else if (option && option.hasOwnProperty('default')) {
value = option.default;
}
return value;
}
......
This diff is collapsed.
......@@ -2,56 +2,61 @@
Object.defineProperty(exports, '__esModule', { value: true });
/**
* Created by rockyl on 2020-01-27.
*/
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) {
if (Array.isArray(options)) {
for (var _i = 0, options_1 = options; _i < options_1.length; _i++) {
var option = options_1[_i];
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];
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;
var value = getValue(result, option, key);
result[key] = transform(key, value, option);
}
}
return result;
function transform(key, value, option) {
var sourceValue = value;
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;
}
return item;
});
value = seps;
break;
}
}
else {
item = sep;
}
return item;
});
value = seps;
break;
}
}
result[key] = value;
};
for (var key in options) {
_loop_1(key);
return value;
}
return result;
}
function parseType(typeStr) {
var type = typeStr, generic;
......@@ -84,13 +89,13 @@ function parseVector2(sourceValue) {
}
return value;
}
function getValue(props, options, key) {
function getValue(props, option, key) {
var value;
if (props.hasOwnProperty(key)) {
value = props[key];
}
else if (options && options[key].hasOwnProperty('default')) {
value = options[key].default;
else if (option && option.hasOwnProperty('default')) {
value = option.default;
}
return value;
}
......
This diff is collapsed.
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global['props-compute'] = {}));
}(this, function (exports) { 'use strict';
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global['props-compute'] = {}));
}(this, (function (exports) { 'use strict';
/**
* Created by rockyl on 2020-01-27.
*/
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) {
if (Array.isArray(options)) {
for (var _i = 0, options_1 = options; _i < options_1.length; _i++) {
var option = options_1[_i];
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];
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;
var value = getValue(result, option, key);
result[key] = transform(key, value, option);
}
}
return result;
function transform(key, value, option) {
var sourceValue = value;
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;
}
return item;
});
value = seps;
break;
}
}
else {
item = sep;
}
return item;
});
value = seps;
break;
}
}
result[key] = value;
};
for (var key in options) {
_loop_1(key);
return value;
}
return result;
}
function parseType(typeStr) {
var type = typeStr, generic;
......@@ -86,13 +91,13 @@
}
return value;
}
function getValue(props, options, key) {
function getValue(props, option, key) {
var value;
if (props.hasOwnProperty(key)) {
value = props[key];
}
else if (options && options[key].hasOwnProperty('default')) {
value = options[key].default;
else if (option && option.hasOwnProperty('default')) {
value = option.default;
}
return value;
}
......@@ -103,5 +108,5 @@
Object.defineProperty(exports, '__esModule', { value: true });
}));
})));
//# sourceMappingURL=index.umd.js.map
This diff is collapsed.
......@@ -3,14 +3,17 @@
"version": "1.0.0",
"main": "dist/index.js",
"module": "dist/index.es.js",
"types": "types.d.ts",
"license": "MIT",
"scripts": {
"dev": "rollup -c -w",
"build": "rollup -c"
"build": "rollup -c",
"dts": "dts-bundle-generator -o types.d.ts src/index.ts"
},
"devDependencies": {
"dts-bundle-generator": "^5.4.0",
"rollup-plugin-typescript": "^1.0.1",
"tslib": "^1.11.1",
"typescript": "^3.8.3"
"tslib": "^2.0.1",
"typescript": "^4.0.3"
}
}
......@@ -2,9 +2,8 @@
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/dist" />
</content>
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</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 @@
const genericRegexp = /(\w+)(<(\w+)>)?/;
export function compute(props, options) {
export function compute(props, options: Object | Array<any>) {
let result = props || {};
for (let key in options) {
let sourceValue = getValue(result, options, key);
let value = sourceValue;
if (options) {
if (Array.isArray(options)) {
for (let option of options) {
let key = option.name;
let value = getValue(result, option, key);
result[key] = transform(key, value, option);
}
} else {
for (let key in options) {
let option = options[key];
if (option && option.type) {
let {type, generic} = parseType(option.type);
switch (type) {
case 'vector2':
value = parseVector2(sourceValue);
break;
case 'array':
let seps = sourceValue.split(',');
seps = seps.map(sep => {
let item;
if (generic) {
switch (generic) {
case 'number':
item = parseFloat(sep);
break;
case 'boolean':
item = sep === 'true';
break;
default:
item = sep;
break;
}
} else {
item = sep;
let value = getValue(result, option, key);
result[key] = transform(key, value, option);
}
}
return result;
function transform(key, value, option) {
let sourceValue = value;
if (option && option.type) {
let {type, generic} = parseType(option.type);
switch (type) {
case 'vector2':
value = parseVector2(sourceValue);
break;
case 'array':
let seps = sourceValue.split(',');
seps = seps.map(sep => {
let item;
if (generic) {
switch (generic) {
case 'number':
item = parseFloat(sep);
break;
case 'boolean':
item = sep === 'true';
break;
default:
item = sep;
break;
}
return item;
});
value = seps;
break;
}
} else {
item = sep;
}
return item;
});
value = seps;
break;
}
}
result[key] = value;
return value;
}
return result;
}
export function parseType(typeStr) {
......@@ -81,12 +93,12 @@ export function parseVector2(sourceValue) {
return value;
}
function getValue(props, options, key) {
function getValue(props, option, key) {
let value;
if (props.hasOwnProperty(key)) {
value = props[key];
} else if (options && options[key].hasOwnProperty('default')) {
value = options[key].default;
} else if (option && option.hasOwnProperty('default')) {
value = option.default;
}
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