Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add NUMBER support to directionFactory
  • Loading branch information
aleskafka committed May 9, 2018
commit 120206126341d574c6782f4bb178ef5b001fa093
15 changes: 9 additions & 6 deletions src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ it('allows omitting units for 0', () => {
})
})

it('allows shorthands without unit', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #40

expect(transformCss([['margin', '10 5 0']])).toEqual({
marginTop: 10,
marginRight: 5,
marginBottom: 0,
marginLeft: 5,
})
})

it('transforms strings', () => {
expect(transformCss([['color', 'red']])).toEqual({ color: 'red' })
})
Expand Down Expand Up @@ -154,9 +163,3 @@ it('allows blacklisting shorthands', () => {
)
expect(actualStyles).toEqual({ borderRadius: 50 })
})

it('throws useful errors', () => {
expect(() => transformCss([['margin', '10']])).toThrow(
'Failed to parse declaration "margin: 10"'
)
})
14 changes: 10 additions & 4 deletions src/transforms/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const {
WORD,
COLOR,
LENGTH,
NUMBER,
UNSUPPORTED_LENGTH_UNIT,
PERCENT,
AUTO,
Expand All @@ -24,7 +25,7 @@ const background = tokenStream => ({
})
const border = anyOrderFactory({
borderWidth: {
tokens: [LENGTH, UNSUPPORTED_LENGTH_UNIT],
tokens: [LENGTH, NUMBER, UNSUPPORTED_LENGTH_UNIT],
default: 1,
},
borderColor: {
Expand All @@ -46,12 +47,17 @@ const borderRadius = directionFactory({
prefix: 'border',
suffix: 'Radius',
})
const borderWidth = directionFactory({ prefix: 'border', suffix: 'Width' })
const borderWidth = directionFactory({
prefix: 'border',
suffix: 'Width'
})
const margin = directionFactory({
types: [LENGTH, UNSUPPORTED_LENGTH_UNIT, PERCENT, AUTO],
types: [LENGTH, NUMBER, UNSUPPORTED_LENGTH_UNIT, PERCENT, AUTO],
prefix: 'margin',
})
const padding = directionFactory({ prefix: 'padding' })
const padding = directionFactory({
prefix: 'padding'
})
const flexFlow = anyOrderFactory({
flexWrap: {
tokens: [regExpToken(/(nowrap|wrap|wrap-reverse)/)],
Expand Down
4 changes: 2 additions & 2 deletions src/transforms/util.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { tokens } from '../tokenTypes'

const { LENGTH, UNSUPPORTED_LENGTH_UNIT, PERCENT, COLOR, SPACE, NONE } = tokens
const { LENGTH, NUMBER, UNSUPPORTED_LENGTH_UNIT, PERCENT, COLOR, SPACE, COMMA, NONE } = tokens

export const directionFactory = ({
types = [LENGTH, UNSUPPORTED_LENGTH_UNIT, PERCENT],
types = [LENGTH, NUMBER, UNSUPPORTED_LENGTH_UNIT, PERCENT],
directions = ['Top', 'Right', 'Bottom', 'Left'],
prefix = '',
suffix = '',
Expand Down