-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
150 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "dist" | ||
"outDir": "dist", | ||
"rootDir": "." | ||
}, | ||
"exclude": ["dist", "node_modules"], | ||
"references": [], | ||
"include": ["src"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
"rootDir": "." | ||
}, | ||
"references": [], | ||
"include": ["src"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# `@artifak/pxtoem` | ||
|
||
> TODO: description | ||
## Usage | ||
|
||
``` | ||
const pxtoem = require('@artifak/pxtoem'); | ||
// TODO: DEMONSTRATE API | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { | ||
pxToEm, | ||
getPxNumericValue, | ||
extractPxNumericValue | ||
} from '../src/pxtoem'; | ||
|
||
describe('@artifak/pxtoem', () => { | ||
it('returns value of "0em" when provided with false-like args', () => { | ||
expect(pxToEm(void 0 as any)).toEqual('0em'); | ||
expect(pxToEm(null as any)).toEqual('0em'); | ||
expect(pxToEm(false as any)).toEqual('0em'); | ||
expect(pxToEm(NaN as any)).toEqual('0em'); | ||
}); | ||
|
||
it('returns a string EM value of 3 when provided a numeric value of "48"', () => { | ||
expect(pxToEm(48)).toEqual('3em'); | ||
}); | ||
|
||
it('returns a string EM value of 3 when provided with a string value of "48PX"', () => { | ||
expect(pxToEm('48px')).toEqual('3em'); | ||
}); | ||
}); | ||
|
||
describe('getPxNumericValue', () => { | ||
it('should return only the numeric type value when given a "PX" numeric value', () => { | ||
expect(getPxNumericValue('1px')).toEqual(1); | ||
}); | ||
|
||
it('should return only the numeric type value when given a "EM" numeric value', () => { | ||
expect(getPxNumericValue('2em')).toEqual(2); | ||
}); | ||
|
||
it('should return only the numeric type value when given a "REM" numeric value', () => { | ||
expect(getPxNumericValue('3rem')).toEqual(3); | ||
}); | ||
|
||
it('should return only the numeric type value when given a "DPI" numeric value', () => { | ||
expect(getPxNumericValue('4dpi')).toEqual(4); | ||
}); | ||
|
||
it('should return only the numeric type value when given a "PT" numeric value', () => { | ||
expect(getPxNumericValue('5pt')).toEqual(5); | ||
}); | ||
}); | ||
|
||
describe('extractPxNumericValue', () => { | ||
it('should return only the numeric type value when given a float string value', () => { | ||
expect(extractPxNumericValue('0.123px')).toEqual(0.123); | ||
}); | ||
|
||
it('should return only the numeric type value when given an integer string value', () => { | ||
expect(extractPxNumericValue('1px')).toEqual(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"name": "@artifak/pxtoem", | ||
"version": "1.0.0", | ||
"description": "Now I’m the model of a modern major general / The venerated Virginian veteran whose men are all / Lining up, to put me up on a pedestal / Writin’ letters to relatives / Embellishin’ my elegance and eloquence / But the elephant is in the room / The truth is in ya face when ya hear the British cannons go / BOOM", | ||
"keywords": [], | ||
"author": "heyjul3s <julian.cy.low@gmail.com>", | ||
"license": "ISC", | ||
"sideEffects": false, | ||
"module": "dist/pxtoem.esm.js", | ||
"main": "dist/pxtoem.cjs.js", | ||
"src": "src/index.ts", | ||
"types": "dist/src/index.d.ts", | ||
"directories": { | ||
"src": "src", | ||
"test": "__tests__" | ||
}, | ||
"files": [ | ||
"dist" | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/heyjul3s/artifak.git" | ||
}, | ||
"scripts": { | ||
"build": "rimraf dist && builder", | ||
"test": "echo \"Error: run tests from root\" && exit 1" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/heyjul3s/artifak/issues" | ||
}, | ||
"homepage": "https://github.com/heyjul3s/artifak#readme", | ||
"devDependencies": { | ||
"@artifak/bundler": "^1.1.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { pxToEm } from '../src/pxtoem'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export function pxToEm(size: number | string, baseSize = 16): string { | ||
const pxValue = getPxNumericValue(size); | ||
return `${pxValue / baseSize}em`; | ||
} | ||
|
||
export function getPxNumericValue(size: number | string): number { | ||
if (Object.prototype.toString.call(size) === '[object String]') { | ||
return extractPxNumericValue(size as string); | ||
} | ||
|
||
if (!!size) { | ||
return size as number; | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
export function extractPxNumericValue(size: string): number { | ||
const WORD_REGEX = /(\D*)$/i; | ||
return Number(size.replace(WORD_REGEX, '')); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
"rootDir": "." | ||
}, | ||
"references": [], | ||
"include": ["src"] | ||
} |