-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added Types API * Improved create-version script * Updated devDependencies
- Loading branch information
Showing
8 changed files
with
2,968 additions
and
1,317 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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([]); | ||
}); | ||
}); |