Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## 1.0.8 – 2016-12-28
✨ Second release v1.0.8
Bugfix:
- **`random()`** method can now be called multiple times within a single property

Minor Changes:
- added comments here and there
- added several badges to readme.md

## 1.0.7 - 2016-10-28
Bugfix:
- removed default randomSeed
Expand Down
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
# postcss-random [![Build Status](https://travis-ci.org/git-slim/postcss-random.svg?branch=develop)](https://travis-ci.org/git-slim/postcss-random)
# postcss-random
<!-- badge -->
[![travis status](https://img.shields.io/travis/git-slim/postcss-random.svg)](https://travis-ci.org/git-slim/postcss-random)
[![npm version](https://img.shields.io/npm/v/postcss-random.svg)](https://www.npmjs.com/package/postcss-random)
[![npm download](https://img.shields.io/npm/dm/postcss-random.svg)](https://www.npmjs.com/package/postcss-random)
[![npm download](https://img.shields.io/npm/dt/postcss-random.svg)](https://www.npmjs.com/package/postcss-random)
[![GitHub stars](https://img.shields.io/github/stars/git-slim/postcss-random.svg?style=social&label=Star)](https://github.com/git-slim/postcss-random)
[![GitHub issues](https://img.shields.io/github/issues/git-slim/postcss-random.svg)](https://github.com/git-slim/postcss-random/issues)
<!-- endbadge -->

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

Expand Down
137 changes: 79 additions & 58 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ module.exports = postcss.plugin( 'postcss-random', function ( options ) {
};

/*---------- global functions ----------*/
function setDefaultRandomOptions(){
randomOptions = {
randomSeed : options['randomSeed'] || null,
round : Boolean(options['round']) || false,
noSeed : Boolean(options['noSeed']) || false,
floatingPoint : parseInt(options['floatingPoint']) || 5,
};
}

// essential random function, returns value depending on setted randomOptions
function getRandom(){
Expand All @@ -54,7 +62,6 @@ module.exports = postcss.plugin( 'postcss-random', function ( options ) {
if( randomOptions.round ){
returnValue = Math.round( returnValue );
}

return returnValue;
}

Expand Down Expand Up @@ -88,6 +95,9 @@ module.exports = postcss.plugin( 'postcss-random', function ( options ) {
function setOptions( argument){
var customOptions;

// reset randomOptions to default
setDefaultRandomOptions();

// parse options, warn if invalid
try{
eval( 'customOptions =' + argument );
Expand Down Expand Up @@ -131,66 +141,77 @@ module.exports = postcss.plugin( 'postcss-random', function ( options ) {
// try to get arguments
try {
// first we get the whole random command
var commandString = value.match( /random\(([^)]+)\)/ )[ 1 ];
// seccond we replace the part ,{ with a bar
var objectTemp = commandString.replace(/,\s*{/,'|');
// third we split it in half to seperate min/max and options
var segmentSplit = objectTemp.split('|');
// if length > 2 then there is something wrong
if( segmentSplit.length > 2){
console.warn( warnings.invalidOptionsFormat, commandString );
return;
}else if( segmentSplit.length === 2){
// otherwise split out min/max
var minMaxSegment = segmentSplit[0];
funcArguments = minMaxSegment.split( ',' );
funcArguments.push( '{' + segmentSplit[1] );
}else{
// and of only one argument exists then it means taht only options were passed
funcArguments = segmentSplit;
var commands = value.match( /random\(([^)]+)\)/g );
// loop over each command instance
for(var i = 0; i < commands.length; i++){
// current command
var curCommand = commands[i];
// minMaxSegment
var minMaxSegment = [];
// command inner
var commandInner = curCommand.match( /random\(([^)]+)\)/ )[ 1 ];
// seccond we replace the part ,{ with a bar
var objectTemp = commandInner.replace(/,\s*{/,'|');
// third we split it in half to seperate min/max and options
var segmentSplit = objectTemp.split('|');
// if length > 2 then there is something wrong
if( segmentSplit.length > 2){
console.warn( warnings.invalidOptionsFormat, commandInner );
return;
}else if( segmentSplit.length === 2){
// set funcArguments based on min & max values as well as options
minMaxSegment = segmentSplit[0];
funcArguments = minMaxSegment.split( ',' );
funcArguments.push( '{' + segmentSplit[1] );
}else{
// set funcArguments based on min & max values
minMaxSegment = segmentSplit[0];
funcArguments = minMaxSegment.split( ',' );
}

// set limits
if( funcArguments.length >= 2 ){
setLimitValues();
}

// perform action depending on arguments count
switch ( funcArguments.length ) {

case 0:
newValue = seedRandom();
break;

case 1:
setOptions( funcArguments[ 0 ] );
if( typeof randomOptions !== 'object' ){
console.warn( warnings.invalidOptionsFormat, randomOptions );
return;
}else{
newValue = getRandom();
}
break;

case 2:
setDefaultRandomOptions();
newValue = getRandom();
break;

case 3:
setOptions( funcArguments[ 2 ] );
newValue = getRandom();

break;

default:
console.warn( warnings.invalidArguments );
return;
}
// finally replace value with new value
decl.value = decl.value.replace( /random\(([^)]*)\)/, newValue );
}
} catch ( e ) {
funcArguments = [];
}

if( funcArguments.length >= 2 ){
setLimitValues();
console.warn(e);
}

// perform action depending on arguments count
switch ( funcArguments.length ) {

case 0:
newValue = seedRandom();
break;

case 1:
setOptions( funcArguments[ 0 ] );
if( typeof randomOptions !== 'object' ){
console.warn( warnings.invalidOptionsFormat, randomOptions );
return;
}else{
newValue = getRandom();
}
break;

case 2:
newValue = getRandom();
break;

case 3:
setOptions( funcArguments[ 2 ] );
newValue = getRandom();

break;

default:
console.warn( warnings.invalidArguments );
return;
}

// finally replace value with new value
decl.value = decl.value.replace( /random\(([^)]*)\)/, newValue );
}

} );
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postcss-random",
"version": "1.0.7",
"version": "1.0.8",
"description": "PostCSS plugin to generate random seeds using `random()` function",
"keywords": [
"css",
Expand Down