rangen
RanGen is a library for generating different types of data.
Features
- Generate random id
- Generate random integer
- Generate random float
- Generate random boolean
- Get random array element
- Generate random user
- Get images from 500px
- Generate lorem ipsum
- Create custom thumbnails
- Generate github-like avatar
Demo
http://otelnov.github.io/rangen/
Installing
Using npm:
$ npm install rangen
Usage
Node.js
const rg = ;rgid; // RhyDMHO
Browser:
rangen
Typescript
; ; // RhyDMHO
Methods
id( params?: Object | number )
Generate random string, using one of the character set listed below or providing your own with str
key.
Character sets:
- num - '0123456789'
- alpha - 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
- sym - '_-~!@#$%^&*()|}{?></'
- alphanum - 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
- all - 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-~!@#$%^&*()|}{?></'
; const newId = ; // W6hOC2Nconst password = ; // f5HK7-s$Q2$#const _id = ; // df9HN5rf3
params | default value |
---|---|
length (optional) | 7 |
charSet (optional) | 'alphanum' |
str (optional) |
num( min?: number, max?: number )
Generate random number in specific range.
; const newNum = ; // 864
param | default value |
---|---|
min (optional) | 0 |
max (optional) | 9999 |
float( min?: number, max?: number, fixed?: number, str?: boolean )
Generate random floating-point number in specific range.
; ; // 0.9529138279087489 ;// '4.830'
param | default value |
---|---|
min (optional) | 0 |
max (optional) | 1 |
fixed (optional) | |
str (optional) | false |
bool( trueProbability?: number )
Return random boolean value ( true / false ).
; const boolean = ; //true
trueProbability is an optional param if you want to control probability of returned value.
bool(0) will never return true
bool(100) will always return true
bool(30) will return true in about 30% of cases...
random( array: any[] )
Get random array element.
; const randomFruit = ; // apple
user( params?: UserParams )
Generate random user data
; const testUser = ;
The following parameters can be passed:
UserParams count?: number; // default 1 gender?: 'male' | 'female' | 'any'; // default any minAge?: number; // default 18 maxAge?: number; // default 55
Results:
"gender": "male" "name": "title": "mr" "first": "Jerome" "last": "Guzman" "email": "jerome.guzman@example.com" "age": 52 "dob": "1965-03-06T22:00:00.000Z" "registered": "2017-04-27T10:34:22.885Z" "phone": "801-345-7850" "id": "IXvQ2rg"
image( params?: ImageParams ):Promise
Generate set of photos provided by https://500px.com.
Please check 500px terms before using.
; ;
You can pass native 500px parameters. Check 500px API for more details.
;
Results:
"id": 105106007 "user_id": 1913159 "name": "Fight between rainbows" "description": null "camera": "Canon EOS 7D" "lens": "EF400mm f/2.8L IS USM" "focal_length": "400" "iso": "200" "shutter_speed": "1/3200" "aperture": "2.8" "times_viewed": 29268 "rating": 75 "status": 1 "created_at": "2015-04-15T10:48:37-04:00" "category": 11 "location": null "latitude": null "longitude": null "taken_at": "2013-05-26T15:16:02-04:00" "hi_res_uploaded": 0 "for_sale": false "width": 4308 "height": 2872 "votes_count": 2311 "favorites_count": 1480 "comments_count": 190 "nsfw": false "sales_count": 0 "for_sale_date": null "highest_rating": 999 "highest_rating_date": "2015-04-16T08:44:17-04:00" "license_type": 0 "converted": 27 "collections_count": 0 "crop_version": 6 "privacy": false "image_url": "https://drscdn.500px.org/photo/105106007/w%3D600_h%3D600/ef557d0965a18ce8a04d612ed7c884bc?v=6" "images": "size": 600 "url": "https://drscdn.500px.org/photo/105106007/w%3D600_h%3D600/ef557d0965a18ce8a04d612ed7c884bc?v=6" "https_url": "https://drscdn.500px.org/photo/105106007/w%3D600_h%3D600/ef557d0965a18ce8a04d612ed7c884bc?v=6" "format": "jpeg" "url": "/photo/105106007/fight-between-rainbows-by-marco-redaelli" "positive_votes_count": 2311 "converted_bits": 27 "image_format": "jpeg" "user": "id": 1913159 "username": "MarcoRedaelli" "firstname": "Marco" "lastname": "Redaelli" "city": "Busnago" "country": "Italy" "usertype": 0 "fullname": "Marco Redaelli" "userpic_url": "https://pacdn.500px.org/1913159/e190e848e7e54255510dc1fd1518bc935328c7b1/1.jpg?5" "userpic_https_url": "https://pacdn.500px.org/1913159/e190e848e7e54255510dc1fd1518bc935328c7b1/1.jpg?5" "cover_url": "https://pacdn.500px.org/1913159/e190e848e7e54255510dc1fd1518bc935328c7b1/cover_2048.jpg?12" "upgrade_status": 0 "store_on": true "affection": 233267 ...
lorem( params?: LoremParams | number )
Generate lorem ipsum text. Takes words number or obj as params.
; ; // Ut incididunt elit eu ad.; // ['Consectetur id do nulla.', 'Consectetur dolor id consectetur.']; // Ad commodo excepteur sed cillum in. Voluptate sed exercitation cupidatat est ex. Consequat eiusmod voluptate excepteur sunt elit.
param | default value |
---|---|
words (optional) | 10 |
sentences (optional) | 1 |
thumb( params?: ThumbParams )
Create thumbnails with specific size, color and text.
; // by default will return base64 hash generated with canvas.; // set svg: true and selector: 'your-css-selector' // to generate svg;
param | default value |
---|---|
width (optional) | 100 |
height (optional) | 100 |
bgColor (optional) | #eee |
textColor (optional) | #555 |
borderColor (optional) | #555 |
text (optional) | {width}x{height} |
fontSize (optional) | depends on block size |
svg (optional) | false |
selector (optional) |
avatar( params?: AvatarParams )
Generate github-like avatars.
; ;;
param | description | default value |
---|---|---|
width (optional) | 300 | |
height (optional) | 300 | |
size (optional) | matrix size | 3 |
colors (optional) | ['#0A7B83', '#2AA876', '#FFD265', '#F19C65', '#CE4D45'] |
Todo:
- Update lorem() to generate natural text using sentiment analysis
- Update avatar() with real faces
- Add more fields to user data, like location
- Generate random color
- Poker cards
- Random time / date (for now you can use several of random nums)
Contributing
Contributions are very much welcome.