@@ -36,6 +36,14 @@ module.exports = postcss.plugin( 'postcss-random', function ( options ) {
3636 } ;
3737
3838 /*---------- global functions ----------*/
39+ function setDefaultRandomOptions ( ) {
40+ randomOptions = {
41+ randomSeed : options [ 'randomSeed' ] || null ,
42+ round : Boolean ( options [ 'round' ] ) || false ,
43+ noSeed : Boolean ( options [ 'noSeed' ] ) || false ,
44+ floatingPoint : parseInt ( options [ 'floatingPoint' ] ) || 5 ,
45+ } ;
46+ }
3947
4048 // essential random function, returns value depending on setted randomOptions
4149 function getRandom ( ) {
@@ -54,7 +62,6 @@ module.exports = postcss.plugin( 'postcss-random', function ( options ) {
5462 if ( randomOptions . round ) {
5563 returnValue = Math . round ( returnValue ) ;
5664 }
57-
5865 return returnValue ;
5966 }
6067
@@ -88,6 +95,9 @@ module.exports = postcss.plugin( 'postcss-random', function ( options ) {
8895 function setOptions ( argument ) {
8996 var customOptions ;
9097
98+ // reset randomOptions to default
99+ setDefaultRandomOptions ( ) ;
100+
91101 // parse options, warn if invalid
92102 try {
93103 eval ( 'customOptions =' + argument ) ;
@@ -131,66 +141,77 @@ module.exports = postcss.plugin( 'postcss-random', function ( options ) {
131141 // try to get arguments
132142 try {
133143 // first we get the whole random command
134- var commandString = value . match ( / r a n d o m \( ( [ ^ ) ] + ) \) / ) [ 1 ] ;
135- // seccond we replace the part ,{ with a bar
136- var objectTemp = commandString . replace ( / , \s * { / , '|' ) ;
137- // third we split it in half to seperate min/max and options
138- var segmentSplit = objectTemp . split ( '|' ) ;
139- // if length > 2 then there is something wrong
140- if ( segmentSplit . length > 2 ) {
141- console . warn ( warnings . invalidOptionsFormat , commandString ) ;
142- return ;
143- } else if ( segmentSplit . length === 2 ) {
144- // otherwise split out min/max
145- var minMaxSegment = segmentSplit [ 0 ] ;
146- funcArguments = minMaxSegment . split ( ',' ) ;
147- funcArguments . push ( '{' + segmentSplit [ 1 ] ) ;
148- } else {
149- // and of only one argument exists then it means taht only options were passed
150- funcArguments = segmentSplit ;
144+ var commands = value . match ( / r a n d o m \( ( [ ^ ) ] + ) \) / g ) ;
145+ // loop over each command instance
146+ for ( var i = 0 ; i < commands . length ; i ++ ) {
147+ // current command
148+ var curCommand = commands [ i ] ;
149+ // minMaxSegment
150+ var minMaxSegment = [ ] ;
151+ // command inner
152+ var commandInner = curCommand . match ( / r a n d o m \( ( [ ^ ) ] + ) \) / ) [ 1 ] ;
153+ // seccond we replace the part ,{ with a bar
154+ var objectTemp = commandInner . replace ( / , \s * { / , '|' ) ;
155+ // third we split it in half to seperate min/max and options
156+ var segmentSplit = objectTemp . split ( '|' ) ;
157+ // if length > 2 then there is something wrong
158+ if ( segmentSplit . length > 2 ) {
159+ console . warn ( warnings . invalidOptionsFormat , commandInner ) ;
160+ return ;
161+ } else if ( segmentSplit . length === 2 ) {
162+ // set funcArguments based on min & max values as well as options
163+ minMaxSegment = segmentSplit [ 0 ] ;
164+ funcArguments = minMaxSegment . split ( ',' ) ;
165+ funcArguments . push ( '{' + segmentSplit [ 1 ] ) ;
166+ } else {
167+ // set funcArguments based on min & max values
168+ minMaxSegment = segmentSplit [ 0 ] ;
169+ funcArguments = minMaxSegment . split ( ',' ) ;
170+ }
171+
172+ // set limits
173+ if ( funcArguments . length >= 2 ) {
174+ setLimitValues ( ) ;
175+ }
176+
177+ // perform action depending on arguments count
178+ switch ( funcArguments . length ) {
179+
180+ case 0 :
181+ newValue = seedRandom ( ) ;
182+ break ;
183+
184+ case 1 :
185+ setOptions ( funcArguments [ 0 ] ) ;
186+ if ( typeof randomOptions !== 'object' ) {
187+ console . warn ( warnings . invalidOptionsFormat , randomOptions ) ;
188+ return ;
189+ } else {
190+ newValue = getRandom ( ) ;
191+ }
192+ break ;
193+
194+ case 2 :
195+ setDefaultRandomOptions ( ) ;
196+ newValue = getRandom ( ) ;
197+ break ;
198+
199+ case 3 :
200+ setOptions ( funcArguments [ 2 ] ) ;
201+ newValue = getRandom ( ) ;
202+
203+ break ;
204+
205+ default :
206+ console . warn ( warnings . invalidArguments ) ;
207+ return ;
208+ }
209+ // finally replace value with new value
210+ decl . value = decl . value . replace ( / r a n d o m \( ( [ ^ ) ] * ) \) / , newValue ) ;
151211 }
152212 } catch ( e ) {
153- funcArguments = [ ] ;
154- }
155-
156- if ( funcArguments . length >= 2 ) {
157- setLimitValues ( ) ;
213+ console . warn ( e ) ;
158214 }
159-
160- // perform action depending on arguments count
161- switch ( funcArguments . length ) {
162-
163- case 0 :
164- newValue = seedRandom ( ) ;
165- break ;
166-
167- case 1 :
168- setOptions ( funcArguments [ 0 ] ) ;
169- if ( typeof randomOptions !== 'object' ) {
170- console . warn ( warnings . invalidOptionsFormat , randomOptions ) ;
171- return ;
172- } else {
173- newValue = getRandom ( ) ;
174- }
175- break ;
176-
177- case 2 :
178- newValue = getRandom ( ) ;
179- break ;
180-
181- case 3 :
182- setOptions ( funcArguments [ 2 ] ) ;
183- newValue = getRandom ( ) ;
184-
185- break ;
186-
187- default :
188- console . warn ( warnings . invalidArguments ) ;
189- return ;
190- }
191-
192- // finally replace value with new value
193- decl . value = decl . value . replace ( / r a n d o m \( ( [ ^ ) ] * ) \) / , newValue ) ;
194215 }
195216
196217 } ) ;
0 commit comments