Skip to content

Commit

Permalink
CY-1489 - Add Types API (#23)
Browse files Browse the repository at this point in the history
* Added Types API
* Improved create-version script
* Updated devDependencies
  • Loading branch information
qooban authored Jan 7, 2020
1 parent 27d3b2d commit c709b67
Show file tree
Hide file tree
Showing 8 changed files with 2,968 additions and 1,317 deletions.
8 changes: 8 additions & 0 deletions documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@ toc:
- getEventIcon
- getNodeIcon
- getNodeStatusIcon
- name: types
description: |
Types API provides helper functions to support type conversions.
children:
- getStringValue
- getTypedValue
- toCloudifyType
- toType

4,001 changes: 2,705 additions & 1,296 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,23 @@
"eslint-config-prettier": "^6.4.0"
},
"devDependencies": {
"@babel/core": "^7.5.4",
"@babel/preset-env": "^7.5.4",
"babel-jest": "^24.8.0",
"documentation": "^12.0.2",
"@babel/core": "^7.7.7",
"@babel/preset-env": "^7.7.7",
"babel-jest": "^24.9.0",
"documentation": "^12.1.4",
"eslint": "^6.7.2",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-jsdoc": "^15.5.3",
"eslint-plugin-prettier": "^3.1.1",
"eslint-plugin-scanjs-rules": "^0.2.1",
"eslint-plugin-security": "^1.4.0",
"jest": "^24.8.0",
"prettier": "^1.18.2",
"rollup": "^1.16.7",
"jest": "^24.9.0",
"prettier": "^1.19.1",
"rollup": "^1.28.0",
"rollup-plugin-babel": "^4.3.3",
"rollup-plugin-commonjs": "^10.0.1",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-terser": "^5.1.1"
"rollup-plugin-terser": "^5.1.3"
},
"peerDependencies": {
"eslint": "^6.7.2",
Expand Down
2 changes: 2 additions & 0 deletions scripts/create-version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ createVersion() {
log "Running tests and build..."
npm run test || {
log "Running tests failed"
false
}
npm run build || {
log "Running build failed"
false
}

NEXT_VERSION=$(npm version --no-git-tag-version "$@")
Expand Down
96 changes: 84 additions & 12 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
- [Parameters][7]
- [getNodeStatusIcon][8]
- [Parameters][9]
- [types][10]
- [getStringValue][11]
- [Parameters][12]
- [getTypedValue][13]
- [Parameters][14]
- [toCloudifyType][15]
- [Parameters][16]
- [toType][17]
- [Parameters][18]

## consts

Expand All @@ -21,11 +30,11 @@ Common constants.

Node statuses constants.

Type: [string][10]
Type: [string][19]

## icons

Icons API intends to provide helper functions to access icons defined within [cloudify][11] font.
Icons API intends to provide helper functions to access icons defined within [cloudify][20] font.


### getEventIcon
Expand All @@ -34,31 +43,76 @@ Get event type icon character to be used with cloudify font.

#### Parameters

- `eventType` **[string][10]** event type, eg. "workflow_started" or "task_received".
- `eventType` **[string][19]** event type, eg. "workflow_started" or "task_received".

Returns **[string][10]** character from cloudify font
Returns **[string][19]** character from cloudify font

### getNodeIcon

Get node type icon character to be used with cloudify font.

#### Parameters

- `hierarchy` **([string][10] \| [Array][12]<[string][10]>)** node type hierarchy, from the most specific to the most generic,
- `hierarchy` **([string][19] \| [Array][21]<[string][19]>)** node type hierarchy, from the most specific to the most generic,
can be single string (eg. "cloudify.nodes.Root")
or array of strings (eg. ["cloudify.nodes.CloudifyManager", "cloudify.nodes.SoftwareComponent", "cloudify.nodes.Root"]).

Returns **[string][10]** character from cloudify font
Returns **[string][19]** character from cloudify font

### getNodeStatusIcon

Get node status icon character to be used with cloudify font.

#### Parameters

- `nodeStatus` **[nodeStatuses][13]** node status value.
- `nodeStatus` **[nodeStatuses][22]** node status value.

Returns **[string][10]** character from cloudify font or empty string for not [nodeStatuses][2] value
Returns **[string][19]** character from cloudify font or empty string for not [nodeStatuses][2] value

## types

Types API provides helper functions to support type conversions.


### getStringValue

Get string value of provided variable.

#### Parameters

- `value` **any** any type variable

Returns **[string][19]** stringified value of provided variable

### getTypedValue

Get typed value of provided string variable.

#### Parameters

- `value` **[string][19]** any string value

Returns **any** typed value of provided string

### toCloudifyType

Get type, used in Cloudify core, of provided variable.

#### Parameters

- `obj` **any** any variable

Returns **[string][19]** variable Cloudify type name

### toType

Get type name of provided variable.

#### Parameters

- `obj` **any** any variable

Returns **[string][19]** variable type name

[1]: #consts

Expand All @@ -78,10 +132,28 @@ Returns **[string][10]** character from cloudify font or empty string for not [n

[9]: #parameters-2

[10]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
[10]: #types

[11]: #getstringvalue

[12]: #parameters-3

[13]: #gettypedvalue

[14]: #parameters-4

[15]: #tocloudifytype

[16]: #parameters-5

[17]: #totype

[18]: #parameters-6

[19]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String

[11]: #fonts
[20]: #fonts

[12]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
[21]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array

[13]: #nodestatuses
[22]: #nodestatuses
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// eslint-disable-next-line import/prefer-default-export
export { default as icons } from './icons'; // to re-export default
export { default as consts } from './consts'; // to re-export default
export { default as types } from './types'; // to re-export default
113 changes: 113 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* Get type name of provided variable.
*
* @returns {string} variable type name
* @param {any} obj any variable
*/
export function toType(obj) {
return {}.toString
.call(obj)
.match(/\s([a-zA-Z]+)/)[1]
.toLowerCase();
}

/**
* Get type, used in Cloudify core, of provided variable.
*
* @returns {string} variable Cloudify type name
* @param {any} obj any variable
*/
export function toCloudifyType(obj) {
const type = toType(obj);

switch (type) {
case 'boolean':
case 'string':
return type;
case 'number':
return Number.isInteger(obj) ? 'integer' : 'float';
case 'array':
return 'list';
case 'object':
return 'dict';
default:
return undefined;
}
}

/**
* Get string value of provided variable.
*
* @param {any} value - any type variable
* @returns {string} stringified value of provided variable
*/
export function getStringValue(value) {
let ret = null;

switch (toType(value)) {
case 'array':
case 'object':
ret = JSON.stringify(value);
break;
case 'boolean':
case 'string':
case 'number':
default:
ret = String(value);
break;
}

return ret;
}

/**
* Get typed value of provided string variable.
*
* @param {string} value - any string value
* @returns {any} typed value of provided string
*/
export function getTypedValue(value) {
const initialType = toType(value);

if (initialType === 'string') {
// Null or Undefined
if (value === 'null') {
return null;
}
if (value === 'undefined') {
return undefined;
}

// Boolean
if (value === 'true') {
return true;
}
if (value === 'false') {
return false;
}

// Number
const numericValue = Number(value);
if (!Number.isNaN(numericValue)) {
return numericValue;
}

// Object or Array
let jsonValue = null;
try {
jsonValue = JSON.parse(value);
} catch (e) {
return value;
}

return jsonValue;
}
return value;
}

export default {
toType,
toCloudifyType,
getStringValue,
getTypedValue
};
46 changes: 46 additions & 0 deletions test/types.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { toType, toCloudifyType, getTypedValue, getStringValue } from '../src/types';

describe('types.js', () => {
it('toType provides correct type of argument', () => {
expect(toType([])).toEqual('array');
expect(toType({})).toEqual('object');
expect(toType(false)).toEqual('boolean');
expect(toType('test')).toEqual('string');
expect(toType(6.4)).toEqual('number');
expect(toType(null)).toEqual('null');
expect(toType(undefined)).toEqual('undefined');
});

it('toCloudifyType provides correct type of argument', () => {
expect(toCloudifyType([])).toEqual('list');
expect(toCloudifyType({})).toEqual('dict');
expect(toCloudifyType(false)).toEqual('boolean');
expect(toCloudifyType('test')).toEqual('string');
expect(toCloudifyType(14)).toEqual('integer');
expect(toCloudifyType(6.4)).toEqual('float');
expect(toCloudifyType(null)).toEqual(undefined);
expect(toCloudifyType(undefined)).toEqual(undefined);
});

it('getStringValue provides string value of argument', () => {
expect(getStringValue([])).toEqual('[]');
expect(getStringValue({})).toEqual('{}');
expect(getStringValue(false)).toEqual('false');
expect(getStringValue('test')).toEqual('test');
expect(getStringValue(6.4)).toEqual('6.4');
expect(getStringValue(null)).toEqual('null');
expect(getStringValue(undefined)).toEqual('undefined');
});

it('getTypedValue provides typed value of string argument', () => {
expect(getTypedValue('[]')).toEqual([]);
expect(getTypedValue('{}')).toEqual({});
expect(getTypedValue('false')).toEqual(false);
expect(getTypedValue('true')).toEqual(true);
expect(getTypedValue('test')).toEqual('test');
expect(getTypedValue('6.4')).toEqual(6.4);
expect(getTypedValue('null')).toEqual(null);
expect(getTypedValue('undefined')).toEqual(undefined);
expect(getTypedValue([])).toEqual([]);
});
});

0 comments on commit c709b67

Please sign in to comment.