Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

Commit 15b5f86

Browse files
committed
switch to error based validation
1 parent b076fd5 commit 15b5f86

File tree

3 files changed

+80
-63
lines changed

3 files changed

+80
-63
lines changed

src/GridNotation.coffee

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class GridNotation
1515
@cmd.unit = @unit
1616
guides = []
1717
tested = @validate(@objectify(string))
18-
return null if !tested.isValid
18+
return null if tested.errors.length > 0
1919

2020
gn = tested.obj
2121
for key, variable of gn.variables
@@ -191,22 +191,23 @@ class GridNotation
191191
#
192192
# Returns an Object.
193193
validate: (obj) =>
194-
isValid = if obj.grids.length > 0 then true else false
195194
variablesWithWildcards = {}
195+
errors = []
196+
error(2, errors) if obj.grids.length <= 0
196197

197198
for key, commands of obj.variables
198199
for command in commands
199-
isValid = false if command.isValid is false
200+
error(command.errors, errors) if command.errors.length > 0
200201
id = command.id
201202
variable = obj.variables[id] if id
202203

203204
# If an undefined variable is called, we can't do anything with it.
204-
isValid = command.isValid = false if id and !variable
205+
error(6, errors, command) if id and !variable
205206

206207
# Fills are only meant to be used once, in one place. Including a fill
207208
# in a variable likely means it will be used in multiple places. In
208209
# theory this *could* be used once, but for now, let's just invalidate.
209-
isValid = command.isValid = false if command.isFill
210+
error(5, errors, command) if command.isFill
210211

211212
variablesWithWildcards[key] = true if command.isWildcard
212213

@@ -217,24 +218,24 @@ class GridNotation
217218
first = grid.params.firstOffset
218219
width = grid.params.width
219220
last = grid.params.lastOffset
220-
isValid = false if first and !first.isValid
221-
isValid = false if width and !width.isValid
222-
isValid = false if last and !last.isValid
221+
error(1, errors) if first and first.errors.length > 0
222+
error(1, errors) if width and width.errors.length > 0
223+
error(1, errors) if last and last.errors.length > 0
223224

224225
for command in grid.commands
225-
isValid = false if command.isValid is false
226+
error(command.errors, errors) if command.errors.length > 0
226227
id = command.id
227228
variable = obj.variables[id] if id
228229

229230
# If an undefined variable is called, we can't do anything with it.
230-
isValid = command.isValid = false if id and !variable
231+
error(6, errors, command) if id and !variable
231232

232233
# Since wildcards don't have an inherent value, it's impossible to
233234
# calculate a fill variable containing one.
234235
varHasWildcard = find(variable, (el) -> el.isWildcard).length > 0
235236

236-
if command.isFill and varHasWildcard
237-
isValid = command.isValid = false
237+
# Fill variables cannot contain wildcards
238+
error(3, errors, command) if command.isFill and varHasWildcard
238239

239240
fills++ if command.isFill
240241
varHasFill = find(variable, (el) -> el.isFill).length > 0
@@ -244,11 +245,10 @@ class GridNotation
244245
fills++
245246

246247
# Fills can only be used once.
247-
isValid = command.isValid = false if fills > 1
248-
if id and variable and varHasFill
249-
isValid = command.isValid = false
248+
error(4, errors, command) if fills > 1
249+
error(5, errors, command) if id and variable and varHasFill
250250

251-
isValid: isValid
251+
errors: errors
252252
obj: obj
253253

254254
# Convert a string of command and guide commands into an object.
@@ -319,7 +319,7 @@ class GridNotation
319319
return true if string.indexOf("|") >= 0 # it has pipes
320320
commands = @parseCommands string
321321
return true if commands.length > 1 # it has multiple commands
322-
return true if commands[0].isValid # it has a valid first command
322+
return true if commands[0].errors.length is 0 # it has a valid first command
323323
false
324324

325325
# Convert a grid string into an object.
@@ -569,29 +569,29 @@ class Command
569569
parse: (string = "") ->
570570
string = string.replace /\s/g, ''
571571
if @isGuide string
572-
isValid: true
572+
errors: []
573573
isGuide: true
574574
else if @isVariable string
575575
bits = @variableRegexp.exec string
576-
isValid: true
576+
errors: []
577577
isVariable: true
578578
isFill: @isFill string
579579
id: if bits[1] then "$#{ bits[1] }" else "$"
580580
multiplier: @count string
581581
else if @isExplicit string
582-
isValid: true
582+
errors: []
583583
isExplicit: true
584584
isPercent: @isPercent string
585585
isFill: @isFill string
586586
unit: @unit.parse(string)
587587
multiplier: @count string
588588
else if @isWildcard string
589-
isValid: if @isFill(string) then false else true
589+
errors: if @isFill(string) then [3] else []
590590
isWildcard: true
591591
isFill: @isFill string
592592
multiplier: @count string
593593
else
594-
isValid: false
594+
errors: [1]
595595
string: string
596596

597597
# Output a command as a string. If it is unrecognized, format it properly.
@@ -619,7 +619,7 @@ class Command
619619
string += '*' if command.isFill or command.multiplier > 1
620620
string += command.multiplier if command.multiplier > 1
621621

622-
if command.isValid then string else "{#{ string }}"
622+
if command.errors.length is 0 then string else "{#{ string }}"
623623

624624
# Create a command string without a multiplier
625625
#
@@ -756,6 +756,23 @@ lengthOf = (command, variables) ->
756756
sum += command.unit.value
757757
sum
758758

759+
# Assign an error code to a command and a master list
760+
#
761+
# code - error code to assign
762+
# master - the master error array
763+
# command - the command that caused the error
764+
#
765+
# Returns nothing.
766+
error = (codes, master, command) ->
767+
codes = [codes] if typeof codes is "number"
768+
for code in codes
769+
exists = find(master, ((e, i) -> true if e is code)).length > 0
770+
master.push code if !exists
771+
return unless command
772+
command.errors ||= []
773+
command.isValid = false
774+
exists = find(command.errors, ((e, i) -> true if e is code)).length > 0
775+
command.errors.push code if !exists
759776

760777
if (typeof module != 'undefined' && typeof module.exports != 'undefined')
761778
module.exports =

test/test-commands.coffee

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,38 +68,38 @@ describe 'Commands', ->
6868

6969
it 'should parse guides', ->
7070
assert.deepEqual Command.parse("|"),
71-
isValid: true
71+
errors: []
7272
isGuide: true
7373

7474
it 'should parse variables', ->
7575
assert.deepEqual Command.parse("$"),
76-
isValid: true
76+
errors: []
7777
isVariable: true
7878
isFill: false
7979
id: "$"
8080
multiplier: 1
8181
assert.deepEqual Command.parse("$foo*2"),
82-
isValid: true
82+
errors: []
8383
isVariable: true
8484
isFill: false
8585
id: "$foo"
8686
multiplier: 2
8787

8888
it 'should parse wildcards', ->
8989
assert.deepEqual Command.parse("~"),
90-
isValid: true
90+
errors: []
9191
isWildcard: true
9292
isFill: false
9393
multiplier: 1
9494
assert.deepEqual Command.parse("~*2"),
95-
isValid: true
95+
errors: []
9696
isWildcard: true
9797
isFill: false
9898
multiplier: 2
9999

100100
it 'should parse explicit', ->
101101
assert.deepEqual Command.parse("10px*2"),
102-
isValid: true
102+
errors: []
103103
isExplicit: true
104104
isFill: false
105105
isPercent: false
@@ -112,7 +112,7 @@ describe 'Commands', ->
112112

113113
it 'should parse unknown', ->
114114
assert.deepEqual Command.parse("foo"),
115-
isValid: false
115+
errors: [1]
116116
string: "foo"
117117

118118
describe 'Multiples', ->
@@ -148,38 +148,38 @@ describe 'Commands', ->
148148

149149
it 'should convert guide commands to strings', ->
150150
assert.equal "|", Command.toString
151-
isValid: true
151+
errors: []
152152
isGuide: true
153153

154154
it 'should convert variable commands to strings', ->
155155
assert.equal "$", Command.toString
156-
isValid: true
156+
errors: []
157157
isVariable: true
158158
isFill: false
159159
id: "$"
160160
multiplier: 1
161161
assert.equal "$foo*2", Command.toString
162-
isValid: true
162+
errors: []
163163
isVariable: true
164164
isFill: false
165165
id: "$foo"
166166
multiplier: 2
167167

168168
it 'should convert wildcard commands to strings', ->
169169
assert.equal "~", Command.toString
170-
isValid: true
170+
errors: []
171171
isWildcard: true
172172
isFill: false
173173
multiplier: 1
174174
assert.equal "~*2", Command.toString
175-
isValid: true
175+
errors: []
176176
isWildcard: true
177177
isFill: false
178178
multiplier: 2
179179

180180
it 'should convert explicit commands to strings', ->
181181
assert.equal "10px*2", Command.toString
182-
isValid: true
182+
errors: []
183183
isExplicit: true
184184
isFill: false
185185
isPercent: false
@@ -193,7 +193,7 @@ describe 'Commands', ->
193193
it 'should simplify strings', ->
194194
assert.equal "~", Command.toSimpleString("~*2")
195195
assert.equal "~", Command.toSimpleString
196-
isValid: true
196+
errors: []
197197
isWildcard: true
198198
isFill: false
199199
multiplier: 2

test/test-grid-notation.coffee

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ describe 'Grid Notation', ->
144144
assert.deepEqual GN.parseVariable("$ = |"),
145145
id: "$"
146146
commands: [
147-
isValid: true
147+
errors: []
148148
isGuide: true
149149
]
150150

@@ -160,25 +160,25 @@ describe 'Grid Notation', ->
160160
assert.strictEqual GN.parseCommands("").length, 0
161161

162162
it 'should parse unknown commands', ->
163-
assert.equal GN.parseCommands("foo")[0].isValid, false
163+
assert GN.parseCommands("foo")[0].errors.length > 0
164164

165165
it 'should parse guide commands', ->
166-
assert GN.parseCommands("|")[0].isValid
166+
assert GN.parseCommands("|")[0].errors.length is 0
167167

168168
it 'should parse arbitrary commands', ->
169-
assert GN.parseCommands("10px")[0].isValid
170-
assert GN.parseCommands("10px*")[0].isValid
171-
assert GN.parseCommands("10px*2")[0].isValid
169+
assert GN.parseCommands("10px")[0].errors.length is 0
170+
assert GN.parseCommands("10px*")[0].errors.length is 0
171+
assert GN.parseCommands("10px*2")[0].errors.length is 0
172172

173173
it 'should parse variable commands', ->
174-
assert GN.parseCommands("$")[0].isValid
175-
assert GN.parseCommands("$A*")[0].isValid
176-
assert GN.parseCommands("$foo*2")[0].isValid
174+
assert GN.parseCommands("$")[0].errors.length is 0
175+
assert GN.parseCommands("$A*")[0].errors.length is 0
176+
assert GN.parseCommands("$foo*2")[0].errors.length is 0
177177

178178
it 'should parse wildcard commands', ->
179-
assert GN.parseCommands("~")[0].isValid
180-
assert GN.parseCommands("~*")[0].isValid is false
181-
assert GN.parseCommands("~*2")[0].isValid
179+
assert GN.parseCommands("~")[0].errors.length is 0
180+
assert GN.parseCommands("~*")[0].errors.length > 0
181+
assert GN.parseCommands("~*2")[0].errors.length is 0
182182

183183
describe 'Parse options', ->
184184

@@ -239,46 +239,46 @@ describe 'Grid Notation', ->
239239

240240
it 'should reject fills in variables', ->
241241
obj = GN.objectify("$ = 10px*")
242-
assert.equal GN.validate(obj).isValid, false
242+
assert GN.validate(obj).errors.length > 0
243243

244244
it 'should reject empty grids', ->
245245
obj = GN.objectify("")
246-
assert.equal GN.validate(obj).isValid, false
246+
assert GN.validate(obj).errors.length > 0
247247

248248
it 'should reject fill variables containing wildcards', ->
249249
obj = GN.objectify """
250250
$ = ~
251251
$*
252252
"""
253-
assert.equal GN.validate(obj).isValid, false
253+
assert GN.validate(obj).errors.length > 0
254254

255255
it 'should reject undefined variables in variables', ->
256256
obj = GN.objectify "$ = $a"
257-
assert.equal GN.validate(obj).isValid, false
257+
assert GN.validate(obj).errors.length > 0
258258

259259
it 'should reject undefined variables in grids', ->
260260
obj = GN.objectify """
261261
$ = 10px
262262
$a
263263
"""
264-
assert.equal GN.validate(obj).isValid, false
264+
assert GN.validate(obj).errors.length > 0
265265

266266
it 'should reject multiple fills in grids', ->
267267
obj = GN.objectify "|10px*|10px*|"
268-
assert.equal GN.validate(obj).isValid, false
268+
assert GN.validate(obj).errors.length > 0
269269

270270
it 'should reject fills if a variable already contains one', ->
271271
obj = GN.objectify """
272272
$ = 10px*
273273
|10px*|
274274
"""
275-
assert.equal GN.validate(obj).isValid, false
275+
assert GN.validate(obj).errors.length > 0
276276

277277
it 'should reject bad alignment params', ->
278-
assert GN.validate(GN.objectify("~ ( ~ | ~ | ~ )")).isValid
279-
assert !GN.validate(GN.objectify("~ ( foo | ~ | ~ )")).isValid
280-
assert !GN.validate(GN.objectify("~ ( ~ | foo | ~ )")).isValid
281-
assert !GN.validate(GN.objectify("~ ( ~ | ~ | foo )")).isValid
278+
assert GN.validate(GN.objectify("~ ( ~ | ~ | ~ )")).errors.length is 0
279+
assert GN.validate(GN.objectify("~ ( foo | ~ | ~ )")).errors.length > 0
280+
assert GN.validate(GN.objectify("~ ( ~ | foo | ~ )")).errors.length > 0
281+
assert GN.validate(GN.objectify("~ ( ~ | ~ | foo )")).errors.length > 0
282282

283283
describe 'Utilities', ->
284284

@@ -287,18 +287,18 @@ describe 'Grid Notation', ->
287287

288288
it 'should expand commands', ->
289289
given = [
290-
isValid: true
290+
errors: []
291291
isWildcard: true
292292
isFill: false
293293
multiplier: 2
294294
]
295295
expected = [
296-
isValid: true
296+
errors: []
297297
isWildcard: true
298298
isFill: false
299299
multiplier: 1
300300
,
301-
isValid: true
301+
errors: []
302302
isWildcard: true
303303
isFill: false
304304
multiplier: 1

0 commit comments

Comments
 (0)