diff --git a/.size-limit.js b/.size-limit.js index 1667d30..43f1fa1 100644 --- a/.size-limit.js +++ b/.size-limit.js @@ -1,6 +1,6 @@ module.exports = [ { path: 'src/index.js', - limit: '1.78kb', + limit: '1.77kb', }, ]; diff --git a/CHANGELOG.md b/CHANGELOG.md index 12ed727..035b9bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [24.0.1](https://github.com/typographist/core/compare/v24.0.0...v24.0.1) (2019-11-10) + + +### Bug Fixes + +* adjust import for "makeBreakpointsMap" ([8065638](https://github.com/typographist/core/commit/8065638)) + # [24.0.0](https://github.com/typographist/core/compare/v23.0.2...v24.0.0) (2019-11-10) diff --git a/package.json b/package.json index 03e4d3b..8b97f8d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@typographist/core", - "version": "24.0.0", + "version": "24.0.1", "description": "Core API for typographist", "main": "./src/index.js", "types": "./src/index.d.ts", diff --git a/src/index.js b/src/index.js index c00b0fa..6816b4a 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ -var getBreakpointsMap = require('./make-breakpoints-map').getBreakpointsMap; +var makeBreakpointsMap = require('./make-breakpoints-map').makeBreakpointsMap; var modularScale = require('./modular-scale'); exports.ratios = { @@ -21,5 +21,5 @@ exports.ratios = { PHI: 1.618034, }; -exports.getBreakpointsMap = getBreakpointsMap; +exports.makeBreakpointsMap = makeBreakpointsMap; exports.modularScale = modularScale; diff --git a/src/lib.js b/src/lib.js index df55ea6..e3ec0f8 100644 --- a/src/lib.js +++ b/src/lib.js @@ -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); -}; diff --git a/src/test/lib.spec.js b/src/test/lib.spec.js new file mode 100644 index 0000000..4108f5e --- /dev/null +++ b/src/test/lib.spec.js @@ -0,0 +1,86 @@ +const { + deepObjectValues, + flatten, + invariant, + merge, + omit, + isNumeric, +} = require('../lib'); + +describe('merge', () => { + it('merge all transfered objects', () => { + expect(merge({ a: 1 }, { b: 2 }, { c: 3 })).toEqual({ a: 1, b: 2, c: 3 }); + }); +}); + +describe('omit', () => { + it('return object without a parameter passed', () => { + expect(omit('a', { a: 1, b: 2 })).toEqual({ b: 2 }); + }); +}); + +describe('deepObjectValues', () => { + it('return an array of a values from the object', () => { + const config = { + aa: 1, + bb: 2, + cc: { + d: { + x: 9, + }, + }, + dd: { + d: { + y: 9, + }, + }, + }; + + expect(deepObjectValues('d')(config)).toEqual([{ x: 9 }, { y: 9 }]); + }); +}); + +describe('flatten', () => { + it('converts from a multidimensional array to a flat array', () => { + expect(flatten([['1em', '2em'], '17px', '18px', '20px', '22px'])).toEqual([ + '1em', + '2em', + '17px', + '18px', + '20px', + '22px', + ]); + }); +}); + +describe('invariant', () => { + it('return object Error with error message', () => { + expect(invariant).toThrow(); + }); +}); + +describe('isNumeric', () => { + it('return `true` if the number', () => { + expect(isNumeric(14.88)).toBe(true); + }); + + it('return `true` if the string contains number', () => { + expect(isNumeric('111')).toBe(true); + }); + + it('return `true` if string contains number with units', () => { + expect(isNumeric('56.78px')).toBe(false); + }); + + it('return `false` if is not a number', () => { + expect(isNumeric(NaN)).toBe(false); + }); + + it('return `true` if is the boolean', () => { + expect(isNumeric(false)).toBe(false); + }); + + it('return `true` if is Infinity number', () => { + expect(isNumeric(Infinity)).toBe(false); + }); +}); diff --git a/src/test/utils.spec.js b/src/test/utils.spec.js deleted file mode 100644 index 85b4dd5..0000000 --- a/src/test/utils.spec.js +++ /dev/null @@ -1,173 +0,0 @@ -const { - deepObjectValues, - flatten, - invariant, - merge, - omit, - isNumeric, - ratioHasFontSize, - ratioHasAtWord, - ratioHasStep, - hasPx, - isValidStep, -} = require('../lib'); - -describe('merge', () => { - it('merge all transfered objects', () => { - expect(merge({ a: 1 }, { b: 2 }, { c: 3 })).toEqual({ a: 1, b: 2, c: 3 }); - }); -}); - -describe('omit', () => { - it('return object without a parameter passed', () => { - expect(omit('a', { a: 1, b: 2 })).toEqual({ b: 2 }); - }); -}); - -describe('deepObjectValues', () => { - it('return an array of a values from the object', () => { - const config = { - aa: 1, - bb: 2, - cc: { - d: { - x: 9, - }, - }, - dd: { - d: { - y: 9, - }, - }, - }; - - expect(deepObjectValues('d')(config)).toEqual([{ x: 9 }, { y: 9 }]); - }); -}); - -describe('flatten', () => { - it('converts from a multidimensional array to a flat array', () => { - expect(flatten([['1em', '2em'], '17px', '18px', '20px', '22px'])).toEqual([ - '1em', - '2em', - '17px', - '18px', - '20px', - '22px', - ]); - }); -}); - -describe('invariant', () => { - it('return object Error with error message', () => { - expect(invariant).toThrow(); - }); -}); - -describe('isNumeric', () => { - it('return `true` if the number', () => { - expect(isNumeric(14.88)).toBe(true); - }); - - it('return `true` if the string contains number', () => { - expect(isNumeric('111')).toBe(true); - }); - - it('return `true` if string contains number with units', () => { - expect(isNumeric('56.78px')).toBe(false); - }); - - it('return `false` if is not a number', () => { - expect(isNumeric(NaN)).toBe(false); - }); - - it('return `true` if is the boolean', () => { - expect(isNumeric(false)).toBe(false); - }); - - it('return `true` if is Infinity number', () => { - expect(isNumeric(Infinity)).toBe(false); - }); -}); - -describe('ratioHasFontSize', () => { - it('return `false` if the font size does not contain pixels', () => { - expect(ratioHasFontSize('35em at 6')).toEqual(false); - }); - - it('return `true` if contain positive integer mumber with pixels at the beggining of the string', () => { - expect(ratioHasFontSize('35px at 6')).toEqual(true); - }); - - it('return `false` if there are spaces at the beginning of the line', () => { - expect(ratioHasFontSize(' 35px at 6')).toEqual(false); - }); -}); - -describe('ratioHasAtWord', () => { - it('should ratio string has `at` word', () => { - expect(ratioHasAtWord('123px at 7')).toEqual(true); - }); - - it("should doesn't contain `at` word", () => { - expect(ratioHasAtWord('123px 7px')).toEqual(false); - }); -}); - -describe('ratioHasStep', () => { - it('return `true` if it contains a negative integer number at end of a string', () => { - expect(ratioHasStep('123px at -7')).toEqual(true); - }); - - it('return `true` if it contains a floating point number at end of a string', () => { - expect(ratioHasStep('123px at 7.4')).toEqual(true); - }); - - it('return `true` if it contains a nagative floating point number at end of a string', () => { - expect(ratioHasStep('123px at -7.4')).toEqual(true); - }); - - it('return `fase` if it contains a integer number with units at end of a string', () => { - expect(ratioHasStep('123px at 6px')).toEqual(false); - }); - - it('return `fase` if it contains a negative floating point number in px units at end of a string', () => { - expect(ratioHasStep('123px at -6.7px')).toEqual(false); - }); -}); - -describe('hasPx', () => { - it('returns `true` if value contains pixels', () => { - expect(hasPx('12px')).toEqual(true); - }); - - it("returns `false` if value doesn't contain px", () => { - expect(hasPx(12)).toEqual(false); - }); -}); - -describe('isValidStep', () => { - it('should integer support', () => { - expect(isValidStep('12')).toEqual(true); - }); - - it('should floating points support', () => { - expect(isValidStep('1.2')).toEqual(true); - }); - - it('should negative meaning', () => { - expect(isValidStep('-7')).toEqual(true); - }); - - it('should invalid number', () => { - expect(isValidStep('1....2')).toEqual(false); - }); - - it('should negative floating point', () => { - expect(isValidStep('-7.777')).toEqual(true); - }); - - it('return `false` is not numerical value', () => { - expect(isValidStep('myNameIsMax')).toEqual(false); - }); -}); diff --git a/src/test/validate-config.spec.js b/src/test/validate-config.spec.js index ebc3818..7ea7782 100644 --- a/src/test/validate-config.spec.js +++ b/src/test/validate-config.spec.js @@ -1,5 +1,6 @@ const { invalidconfig } = require('./mocks'); const { + hasPx, validateConfig, validateBase, validateBases, @@ -8,10 +9,23 @@ const { validateBreakpoints, validateLineHeight, validateLineHeights, + ratioHasFontSize, + ratioHasAtWord, + ratioHasStep, validateRatio, validateRatios, } = require('../validate-config'); +describe('hasPx', () => { + it('returns `true` if value contains pixels', () => { + expect(hasPx('12px')).toEqual(true); + }); + + it("returns `false` if value doesn't contain px", () => { + expect(hasPx(12)).toEqual(false); + }); +}); + describe('validateBase', () => { it("show warn if the base isn't valid", () => { try { @@ -105,6 +119,52 @@ describe('validateLineHeights', () => { }); }); +describe('ratioHasFontSize', () => { + it('return `false` if the font size does not contain pixels', () => { + expect(ratioHasFontSize('35em at 6')).toEqual(false); + }); + + it('return `true` if contain positive integer mumber with pixels at the beggining of the string', () => { + expect(ratioHasFontSize('35px at 6')).toEqual(true); + }); + + it('return `false` if there are spaces at the beginning of the line', () => { + expect(ratioHasFontSize(' 35px at 6')).toEqual(false); + }); +}); + +describe('ratioHasAtWord', () => { + it('should ratio string has `at` word', () => { + expect(ratioHasAtWord('123px at 7')).toEqual(true); + }); + + it("should doesn't contain `at` word", () => { + expect(ratioHasAtWord('123px 7px')).toEqual(false); + }); +}); + +describe('ratioHasStep', () => { + it('return `true` if it contains a negative integer number at end of a string', () => { + expect(ratioHasStep('123px at -7')).toEqual(true); + }); + + it('return `true` if it contains a floating point number at end of a string', () => { + expect(ratioHasStep('123px at 7.4')).toEqual(true); + }); + + it('return `true` if it contains a nagative floating point number at end of a string', () => { + expect(ratioHasStep('123px at -7.4')).toEqual(true); + }); + + it('return `fase` if it contains a integer number with units at end of a string', () => { + expect(ratioHasStep('123px at 6px')).toEqual(false); + }); + + it('return `fase` if it contains a negative floating point number in px units at end of a string', () => { + expect(ratioHasStep('123px at -6.7px')).toEqual(false); + }); +}); + describe('validateRatio', () => { it("show warn if the ratio value isn't valid", () => { try { diff --git a/src/validate-config.js b/src/validate-config.js index fab7faa..2c17869 100644 --- a/src/validate-config.js +++ b/src/validate-config.js @@ -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, ); }; @@ -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, ); }; @@ -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); }; @@ -109,6 +129,7 @@ var validateConfig = function(x) { }; module.exports = { + hasPx, validateBase, validateBases, hasBreakpointProp, @@ -116,6 +137,9 @@ module.exports = { validateBreakpoints, validateLineHeight, validateLineHeights, + ratioHasFontSize, + ratioHasAtWord, + ratioHasStep, validateRatio, validateRatios, validateConfig,