Skip to content

Commit

Permalink
perf: move some regexes to constants
Browse files Browse the repository at this point in the history
  • Loading branch information
Bo Lingen committed Jan 15, 2019
1 parent 7a1057b commit a87e0a7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/constants.js
@@ -1,4 +1,8 @@
export const MAX_SAFE_INTEGER = 9007199254740991
export const NUMERIC_REGEX = /^((?:\d+)?\.?(?:\d+)?)$/
export const LEFT_RIGHT_WHITESPACE_REGEX = /^\s+|\s+$/g
export const LEFT_RIGHT_DOT_REGEX = /^[.]*|[.]*$/g
export const BRACKETED_REGEX = /\[([^[\]]+)\]/g
export const OCTAL_REGEX = /^0o[0-7]+$/i
export const BAD_HEX_REGEX = /^[-+]0x[0-9a-f]+$/i
export const BINARY_REGEX = /^0b[01]+$/i
Expand Down
3 changes: 2 additions & 1 deletion src/is-numeric.js
@@ -1,8 +1,9 @@
import isString from './is-string'
import isNumber from './is-number'
import { NUMERIC_REGEX } from './constants'

export default function isNumeric (value) {
if (isNumber(value)) return true
if (!isString(value)) return false
return (/^((?:\d+)?\.?(?:\d+)?)$/).test(value)
return NUMERIC_REGEX.test(value)
}
9 changes: 7 additions & 2 deletions src/lib/_normalize-path.js
@@ -1,5 +1,10 @@
import {
BRACKETED_REGEX,
LEFT_RIGHT_DOT_REGEX
} from '../constants'

export default function _normalizePath (path) {
return path
.replace(/\[([^[\]]+)\]/g, '.$1')
.replace(/^[.]*|[.]*$/g, '')
.replace(BRACKETED_REGEX, '.$1') // bracketed path link
.replace(LEFT_RIGHT_DOT_REGEX, '') // leading or trailing dots
}
3 changes: 2 additions & 1 deletion src/to-number.js
Expand Up @@ -2,6 +2,7 @@ import getType from './get-type'
import isArrayLike from './is-array-like'

import {
LEFT_RIGHT_WHITESPACE_REGEX,
BINARY_REGEX,
OCTAL_REGEX,
BAD_HEX_REGEX
Expand All @@ -26,7 +27,7 @@ export default function toNumber (value, round) {
rolling = String(rolling)
}

rolling = rolling.replace(/^\s+|\s+$/g, '')
rolling = rolling.replace(LEFT_RIGHT_WHITESPACE_REGEX, '')

const isBinary = BINARY_REGEX.test(rolling)
if (isBinary || OCTAL_REGEX.test(rolling)) {
Expand Down

0 comments on commit a87e0a7

Please sign in to comment.