Skip to content
This repository has been archived by the owner on Feb 17, 2021. It is now read-only.

Commit

Permalink
refactor: move functions "hasPx",
Browse files Browse the repository at this point in the history
"ratioHasFontSize", "ratioHasAtWord", "ratioHasStep" from lib to
validate-config
  • Loading branch information
mg901 committed Nov 10, 2019
1 parent 2f006a4 commit 78d4987
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 31 deletions.
26 changes: 0 additions & 26 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,33 +68,7 @@ exports.invariant = function(condition, message) {
}
};

// ---------- VALIDATORS -----------------------------------------------------------
// isNumeric :: a -> Boolean
exports.isNumeric = function(x) {
return !Number.isNaN(parseFloat(x)) && isFinite(x);
};

// ratioHasFontSize :: String -> Boolean
exports.ratioHasFontSize = function(x) {
return /^\d+(\.\d+)?px\b/g.test(x);
};

// ratioHasAtWord :: String -> Boolean
exports.ratioHasAtWord = function(x) {
return /\sat\s/.test(x);
};

// ratioHasStep :: String -> Boolean
exports.ratioHasStep = function(x) {
return /-?\b\d+(\.\d+)?\b\s*$/g.test(x);
};

// hasPx :: a -> Boolean
exports.hasPx = function(x) {
return /\d+(\.\d+)?px/.test(x);
};

// isValidStep :: String -> Boolean
exports.isValidStep = function(x) {
return /^-?\d+(\.\d+)?$/.test(x);
};
34 changes: 29 additions & 5 deletions src/validate-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ var RATIO_ERROR_MESSAGE =
"is ivalid 'ratio'. Ratio must be a number or string containing the font size (in pixels), " +
"the word 'at' and step. Example ratio: 1.25 or ratio: '36px at 6'.";

// hasPx :: a -> Boolean
var hasPx = function(x) {
return /\d+(\.\d+)?px/.test(x);
};

// ---------- BASE ------------------------------------------------------------

// validateField :: a -> Void
var validateBase = function(x) {
return utils.invariant(
utils.hasPx(x),
hasPx(x),
ERROR_PREFIX + "'" + x + "' " + BASE_ERROR_MESSAGE,
);
};
Expand Down Expand Up @@ -52,7 +58,7 @@ var hasBreakpointProp = function(x) {
// validateField :: a -> Void
var validateBreakpoint = function(x) {
utils.invariant(
typeof x === 'string' && utils.hasPx(x),
typeof x === 'string' && hasPx(x),
ERROR_PREFIX + "'" + x + "' " + BREAKPOINT_ERROR_MESSAGE,
);
};
Expand Down Expand Up @@ -81,13 +87,27 @@ var validateLineHeights = function(x) {
};

// ---------- RATIO --------------------------------------------------------------

// ratioHasFontSize :: String -> Boolean
var ratioHasFontSize = function(x) {
return /^\d+(\.\d+)?px\b/g.test(x);
};

// ratioHasAtWord :: String -> Boolean
var ratioHasAtWord = function(x) {
return /\sat\s/.test(x);
};

// ratioHasStep :: String -> Boolean
var ratioHasStep = function(x) {
return /-?\b\d+(\.\d+)?\b\s*$/g.test(x);
};

// validateField :: a -> Void
var validateRatio = function(x) {
var isValid =
(typeof x === 'number' && utils.isNumeric(x)) ||
(utils.ratioHasFontSize(x) &&
utils.ratioHasAtWord(x) &&
utils.ratioHasStep(x));
(ratioHasFontSize(x) && ratioHasAtWord(x) && ratioHasStep(x));

utils.invariant(isValid, ERROR_PREFIX + "'" + x + "' " + RATIO_ERROR_MESSAGE);
};
Expand All @@ -109,13 +129,17 @@ var validateConfig = function(x) {
};

module.exports = {
hasPx,
validateBase,
validateBases,
hasBreakpointProp,
validateBreakpoint,
validateBreakpoints,
validateLineHeight,
validateLineHeights,
ratioHasFontSize,
ratioHasAtWord,
ratioHasStep,
validateRatio,
validateRatios,
validateConfig,
Expand Down

0 comments on commit 78d4987

Please sign in to comment.