Screwdriver
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');
}
}
`
Polyfill
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();
}
``
MATH
$3
Returns a random float number between min and max, included.
$3
Returns a random integer between min and max, included. Each Integer have the same distribution.
ARRAY
$3
Returns array with unique values
$3
Returns true if value is in the array
$3
Execute a function for each element of an array
TIME
$3
Get the date and time with the format "Y-m-d H:i:s". Optional time is aimed timestamp in ms
$3
Get the time now (timestamp in ms) or time relative to now. Ex: sd.now(24
6060) is now + 1 day
FILE
$3
Get the extension of a file path
$3
Get the filename (name and extension) of a file path
$3
Get the directory of a file path (without the filename)
$3
Get the filename without the extension of a path
$3
Synchronous mkdirp, create directory and parent directories if needed, similar to command "mkdir -p"
$3
Create directory and parent directories if needed, similar to command "mkdir -p"
$3
Returns true if the path exists and is a file
$3
Returns true if the path exists and is a directory
TYPE
$3
Returns true if the value is an integer
$3
Returns true if the value is an array
$3
Returns true if the value is a boolean
$3
Returns true if the value is undefined
$3
Returns true if the value is defined
$3
Returns true if the value is a string
$3
Returns true if the value is a regular expression
$3
Returns true if the value is a function
$3
Returns true if the value is an object
$3
Returns true if the string value is a hexadecimal HTML color (#FFFFFF)
$3
Returns true if the string value is a short hexadecimal HTML color (#FFF)
STRING
$3
Return trimmed string, remove left and right space characters
$3
Return left trimmed string, remove left space characters
$3
Return right trimmed string, remove right space characters
$3
Return trimmed string and replace multiple space characters with a single space character
$3
Returns true if the search is contained in str
$3
Returns true if the search is at the end of str
$3
Returns true if str begins with search, optional starts at position
$3
Returns the string with the first character in upper case
VECTOR
$3
Converts degree to radian
$3
Converts radian to degree
$3
Return a normalize vector object
COLOR
$3
Converts hexadecimal HTML color (#FFF or #FFFFFF) to RGB object {r,g,b}
$3
Converts RGB object {r,g,b} to hexadecimal HTML color (#FFFFFF)
$3
Converts short hexadecimal HTML color (#FFF) to full hexadecimal HTML color (#FFFFFF) if needed
Changelog
$3
- Add parameter to getDateTime to change date
- Touch file
$3
- ucFirst function
$3
- Polyfill function
- Fix small bugs
- Add type functions to Object