From 7fb5ca65175343ce04ddf5eadf06e5c690165a34 Mon Sep 17 00:00:00 2001 From: jos Date: Wed, 13 Jul 2016 14:32:43 +0200 Subject: [PATCH] Some refactoring --- src/JSONNode.js | 8 +- src/utils/{getInnerText.js => domUtils.js} | 2 +- src/utils/{parseJSON.js => jsonUtils.js} | 2 +- src/utils/objectUtils.js | 15 +--- src/utils/stringConvert.js | 32 -------- src/utils/{escape.js => stringUtils.js} | 2 +- src/utils/typeUtils.js | 86 ++++++++++++++++++++++ src/utils/valueType.js | 42 ----------- 8 files changed, 93 insertions(+), 96 deletions(-) rename src/utils/{getInnerText.js => domUtils.js} (97%) rename src/utils/{parseJSON.js => jsonUtils.js} (94%) delete mode 100644 src/utils/stringConvert.js rename src/utils/{escape.js => stringUtils.js} (98%) create mode 100644 src/utils/typeUtils.js delete mode 100644 src/utils/valueType.js diff --git a/src/JSONNode.js b/src/JSONNode.js index 7e6d1eb..483a809 100644 --- a/src/JSONNode.js +++ b/src/JSONNode.js @@ -1,9 +1,7 @@ import { h, Component } from 'preact' -import { isObject } from './utils/objectUtils' -import { escapeHTML, unescapeHTML } from './utils/escape' -import getInnerText from './utils/getInnerText' -import stringConvert from './utils/stringConvert' -import valueType, {isUrl} from './utils/valueType' +import { escapeHTML, unescapeHTML } from './utils/stringUtils' +import { getInnerText } from './utils/domUtils' +import {stringConvert, valueType, isUrl, isObject} from './utils/typeUtils' export default class JSONNode extends Component { constructor (props) { diff --git a/src/utils/getInnerText.js b/src/utils/domUtils.js similarity index 97% rename from src/utils/getInnerText.js rename to src/utils/domUtils.js index 8377a1a..218121e 100644 --- a/src/utils/getInnerText.js +++ b/src/utils/domUtils.js @@ -4,7 +4,7 @@ * @param {Object} [buffer] * @return {String} innerText */ -export default function getInnerText (element, buffer) { +export function getInnerText (element, buffer) { var first = (buffer == undefined) if (first) { buffer = { diff --git a/src/utils/parseJSON.js b/src/utils/jsonUtils.js similarity index 94% rename from src/utils/parseJSON.js rename to src/utils/jsonUtils.js index ec3ace5..1b43c8a 100644 --- a/src/utils/parseJSON.js +++ b/src/utils/jsonUtils.js @@ -4,7 +4,7 @@ * @param {String} jsonString * @return {JSON} json */ -export default function parseJSON(jsonString) { +export function parseJSON(jsonString) { try { return JSON.parse(jsonString) } diff --git a/src/utils/objectUtils.js b/src/utils/objectUtils.js index 5cb43b4..3740360 100644 --- a/src/utils/objectUtils.js +++ b/src/utils/objectUtils.js @@ -1,17 +1,4 @@ - -/** - * Test whether a value is an object (and not an Array or Date or primitive value) - * - * @param {*} value - * @return {boolean} - */ -export function isObject (value) { - return typeof value === 'object' && - value && // not null - !Array.isArray(value) && - value.toString() === '[object Object]' -} - +import { isObject } from './typeUtils' // TODO: unit test getIn /** diff --git a/src/utils/stringConvert.js b/src/utils/stringConvert.js deleted file mode 100644 index 0c0d72c..0000000 --- a/src/utils/stringConvert.js +++ /dev/null @@ -1,32 +0,0 @@ - -/** - * Convert contents of a string to the correct JSON type. This can be a string, - * a number, a boolean, etc - * @param {String} str - * @return {*} castedStr - * @private - */ -export default function stringConvert (str) { - const lower = str.toLowerCase() - const num = Number(str) // will nicely fail with '123ab' - const numFloat = parseFloat(str) // will nicely fail with ' ' - - if (str == '') { - return '' - } - else if (lower == 'null') { - return null - } - else if (lower == 'true') { - return true - } - else if (lower == 'false') { - return false - } - else if (!isNaN(num) && !isNaN(numFloat)) { - return num - } - else { - return str - } -} diff --git a/src/utils/escape.js b/src/utils/stringUtils.js similarity index 98% rename from src/utils/escape.js rename to src/utils/stringUtils.js index cedaeaf..d448688 100644 --- a/src/utils/escape.js +++ b/src/utils/stringUtils.js @@ -1,4 +1,4 @@ -import parseJSON from './parseJSON' +import { parseJSON } from './jsonUtils' /** * escape a text, such that it can be displayed safely in an HTML element diff --git a/src/utils/typeUtils.js b/src/utils/typeUtils.js new file mode 100644 index 0000000..3e9ee9a --- /dev/null +++ b/src/utils/typeUtils.js @@ -0,0 +1,86 @@ + +/** + * Get the type of a value + * @param {*} value + * @return {String} type + */ +export function valueType(value) { + if (value === null) { + return 'null' + } + if (value === undefined) { + return 'undefined' + } + if (typeof value === 'number') { + return 'number' + } + if (typeof value === 'string') { + return 'string' + } + if (typeof value === 'boolean') { + return 'boolean' + } + if (value instanceof RegExp) { + return 'regexp' + } + if (Array.isArray(value)) { + return 'array' + } + + return 'object' +} + +/** + * Test whether a value is an object (and not an Array or Date or primitive value) + * + * @param {*} value + * @return {boolean} + */ +export function isObject (value) { + return typeof value === 'object' && + value && // not null + !Array.isArray(value) && + value.toString() === '[object Object]' +} + +/** + * Test whether a text contains a url (matches when a string starts + * with 'http://*' or 'https://*' and has no whitespace characters) + * @param {String} text + */ +var isUrlRegex = /^https?:\/\/\S+$/ +export function isUrl (text) { + return (typeof text === 'string') && isUrlRegex.test(text) +} + +/** + * Convert contents of a string to the correct JSON type. This can be a string, + * a number, a boolean, etc + * @param {String} str + * @return {*} castedStr + * @private + */ +export function stringConvert (str) { + const lower = str.toLowerCase() + const num = Number(str) // will nicely fail with '123ab' + const numFloat = parseFloat(str) // will nicely fail with ' ' + + if (str == '') { + return '' + } + else if (lower == 'null') { + return null + } + else if (lower == 'true') { + return true + } + else if (lower == 'false') { + return false + } + else if (!isNaN(num) && !isNaN(numFloat)) { + return num + } + else { + return str + } +} diff --git a/src/utils/valueType.js b/src/utils/valueType.js deleted file mode 100644 index 2ea188c..0000000 --- a/src/utils/valueType.js +++ /dev/null @@ -1,42 +0,0 @@ - -/** - * Get the type of a value - * @param {*} value - * @return {String} type - */ -export default function valueType(value) { - if (value === null) { - return 'null' - } - if (value === undefined) { - return 'undefined' - } - if (typeof value === 'number') { - return 'number' - } - if (typeof value === 'string') { - return 'string' - } - if (typeof value === 'boolean') { - return 'boolean' - } - if (value instanceof RegExp) { - return 'regexp' - } - if (Array.isArray(value)) { - return 'array' - } - - return 'object' -} - - -/** - * Test whether a text contains a url (matches when a string starts - * with 'http://*' or 'https://*' and has no whitespace characters) - * @param {String} text - */ -var isUrlRegex = /^https?:\/\/\S+$/ -export function isUrl (text) { - return (typeof text === 'string') && isUrlRegex.test(text) -}