tweak(xod-project): add a lookup table for type casting rules

This commit is contained in:
Evgeny Kochetkov
2017-05-16 20:01:46 +03:00
parent 36b1bf13f5
commit 4c8fedb5d1
2 changed files with 41 additions and 0 deletions

View File

@@ -52,6 +52,45 @@ export const PIN_TYPE = {
PULSE: 'pulse',
};
/**
* A lookup table that answers the
* question 'can a type A be cast to type B?'
*
* @example
* TYPES_COMPATIBILITY[PIN_TYPE.BOOLEAN][PIN_TYPE.STRING] // true
* // boolean can be cast to string
*
* @name TYPES_COMPATIBILITY
*/
export const TYPES_COMPATIBILITY = {
[PIN_TYPE.BOOLEAN]: {
[PIN_TYPE.BOOLEAN]: true,
[PIN_TYPE.NUMBER]: true,
[PIN_TYPE.PULSE]: false,
[PIN_TYPE.STRING]: true,
},
[PIN_TYPE.NUMBER]: {
[PIN_TYPE.BOOLEAN]: true,
[PIN_TYPE.NUMBER]: true,
[PIN_TYPE.PULSE]: false,
[PIN_TYPE.STRING]: true,
},
// nothing can be cast to or from pulse
[PIN_TYPE.PULSE]: {
[PIN_TYPE.BOOLEAN]: false,
[PIN_TYPE.NUMBER]: false,
[PIN_TYPE.PULSE]: true,
[PIN_TYPE.STRING]: false,
},
// everything(except pulse) can be cast to string, but nothing can be cast from string
[PIN_TYPE.STRING]: {
[PIN_TYPE.BOOLEAN]: false,
[PIN_TYPE.NUMBER]: false,
[PIN_TYPE.PULSE]: false,
[PIN_TYPE.STRING]: true,
},
};
/**
* Enumeration of possible pin directions
*

View File

@@ -107,6 +107,8 @@ export const defaultValueOfType = def(
])
);
export const canCastTypes = R.curry((from, to) => CONST.TYPES_COMPATIBILITY[from][to]);
// =============================================================================
//
// Transforming node ids in the patch