A toolkit adding very helpfull functions to your node scripts.
There is some functions missing in Javascript and node scripts, like checking if a file exists or trimming a string, manipulating HTML colors and easily check the type of a variable.
Example in a node js script :
// without polyfill
// Set the sd variable
var sd = require('node-screwdriver');
// set path as a string
var path = './test/myImage.png';
// use the sd methods
console.log('Show extension: '+sd.getExtension(path));
console.log('Show dir: '+sd.getDir(path));
// with polyfill
require('node-screwdriver').polyfill();
// path is still a string but now we can use path.fileExists()
if (path.fileExists()) {
console.log('The file alrady exists');
} else {
if (path.touch()) {
console.log('File created);
} else {
console.log('Could not create the file');
}
}
You can use the function polyfill() to add methods to the javascript objects and prototypes so it's more easy to use.
Added to all instances of the String objects:
- getExtension
- getNoExtension
- getFilename
- getDir
- fileExists
- touch
- dirExists
- isHex
- isHexShort
- trim
- ltrim
- rtrim
- xtrim
- contains
- endWith
- startsWith
- ucFirst
Added to all instances of the Array objects:
- quickSort
require('node-screwdriver').polyfill();
var str = '/path/to/file/myImage.png';
console.log('Extension: '+str.getExtension());
console.log('Is a file: '+str.fileExists());
console.log('File name starts with "~": '+str.getFilename().startsWith('~'));
Added to Math object:
- randFloat
- randInt
require('node-screwdriver').polyfill();
var randomX = Math.randInt(0, 100);
Added to Object:
- isInt
- isFloat
- isDefined
- isUndefined
- isBoolean
- isRegexp
- isString
- isObject
require('node-screwdriver').polyfill();
var str = 'My string';
console.log('Is float: '+Object.isFloat(str);
console.log('Is boolean: '+Object.isBoolean(str);
console.log('Is string: '+Object.isString(str);
You can also use the methods without polyfill.
var sd = require('node-screwdriver');
var path = './MyDir/';
if (!path.dirExists()) {
path.mkdirp();
}
Returns a random float number between min and max, included.
Returns a random integer between min and max, included. Each Integer have the same distribution.
Returns array with unique values
Returns true if value is in the array
Execute a function for each element of an array
Get the date and time with the format "Y-m-d H:i:s". Optional time is aimed timestamp in ms
Get the time now (timestamp in ms) or time relative to now. Ex: sd.now(246060) is now + 1 day
Get the extension of a file path
Get the filename (name and extension) of a file path
Get the directory of a file path (without the filename)
Get the filename without the extension of a path
Synchronous mkdirp, create directory and parent directories if needed, similar to command "mkdir -p"
Create directory and parent directories if needed, similar to command "mkdir -p"
Returns true if the path exists and is a file
Returns true if the path exists and is a directory
Returns true if the value is an integer
Returns true if the value is an array
Returns true if the value is a boolean
Returns true if the value is undefined
Returns true if the value is defined
Returns true if the value is a string
Returns true if the value is a regular expression
Returns true if the value is a function
Returns true if the value is an object
Returns true if the string value is a hexadecimal HTML color (#FFFFFF)
Returns true if the string value is a short hexadecimal HTML color (#FFF)
Return trimmed string, remove left and right space characters
Return left trimmed string, remove left space characters
Return right trimmed string, remove right space characters
Return trimmed string and replace multiple space characters with a single space character
Returns true if the search is contained in str
Returns true if the search is at the end of str
Returns true if str begins with search, optional starts at position
Returns the string with the first character in upper case
Converts degree to radian
Converts radian to degree
Return a normalize vector object
Converts hexadecimal HTML color (#FFF or #FFFFFF) to RGB object {r,g,b}
Converts RGB object {r,g,b} to hexadecimal HTML color (#FFFFFF)
Converts short hexadecimal HTML color (#FFF) to full hexadecimal HTML color (#FFFFFF) if needed
- Add parameter to getDateTime to change date
- Touch file
- ucFirst function
- Polyfill function
- Fix small bugs
- Add type functions to Object