Some refactoring

This commit is contained in:
jos 2016-07-13 14:32:43 +02:00
parent c0f075a9e6
commit 7fb5ca6517
8 changed files with 93 additions and 96 deletions

View File

@ -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) {

View File

@ -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 = {

View File

@ -4,7 +4,7 @@
* @param {String} jsonString
* @return {JSON} json
*/
export default function parseJSON(jsonString) {
export function parseJSON(jsonString) {
try {
return JSON.parse(jsonString)
}

View File

@ -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
/**

View File

@ -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
}
}

View File

@ -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

86
src/utils/typeUtils.js Normal file
View File

@ -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
}
}

View File

@ -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)
}