Skip to content

Commit 1206988

Browse files
authored
Merge pull request #5 from SlimMarten/master
merge master with develop branch
2 parents f49ec42 + 66a9a93 commit 1206988

File tree

4 files changed

+98
-60
lines changed

4 files changed

+98
-60
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
## 1.0.8 – 2016-12-28
2+
✨ Second release v1.0.8
3+
Bugfix:
4+
- **`random()`** method can now be called multiple times within a single property
5+
6+
Minor Changes:
7+
- added comments here and there
8+
- added several badges to readme.md
9+
110
## 1.0.7 - 2016-10-28
211
Bugfix:
312
- removed default randomSeed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
# postcss-random [![Build Status](https://travis-ci.org/git-slim/postcss-random.svg?branch=develop)](https://travis-ci.org/git-slim/postcss-random)
1+
# postcss-random
2+
<!-- badge -->
3+
[![travis status](https://img.shields.io/travis/git-slim/postcss-random.svg)](https://travis-ci.org/git-slim/postcss-random)
4+
[![npm version](https://img.shields.io/npm/v/postcss-random.svg)](https://www.npmjs.com/package/postcss-random)
5+
[![npm download](https://img.shields.io/npm/dm/postcss-random.svg)](https://www.npmjs.com/package/postcss-random)
6+
[![npm download](https://img.shields.io/npm/dt/postcss-random.svg)](https://www.npmjs.com/package/postcss-random)
7+
[![GitHub stars](https://img.shields.io/github/stars/git-slim/postcss-random.svg?style=social&label=Star)](https://github.com/git-slim/postcss-random)
8+
[![GitHub issues](https://img.shields.io/github/issues/git-slim/postcss-random.svg)](https://github.com/git-slim/postcss-random/issues)
9+
<!-- endbadge -->
210

311
> [PostCSS](https://github.com/postcss/postcss) plugin to generate random numbers based on random seeds using `random()` function.
412

index.js

Lines changed: 79 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -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( /random\(([^)]+)\)/ )[ 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( /random\(([^)]+)\)/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( /random\(([^)]+)\)/ )[ 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( /random\(([^)]*)\)/, 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( /random\(([^)]*)\)/, newValue );
194215
}
195216

196217
} );

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-random",
3-
"version": "1.0.7",
3+
"version": "1.0.8",
44
"description": "PostCSS plugin to generate random seeds using `random()` function",
55
"keywords": [
66
"css",

0 commit comments

Comments
 (0)