0.7.18 • Published 3 years ago

aftc-node-tools v0.7.18

Weekly downloads
4
License
ISC
Repository
github
Last release
3 years ago

AFTC NODE TOOLS v0.7.17

A collection of tools/utilities that I find useful when working with node.

Donate

Available Methods & Class's:

  • enableLog()
  • disableLog()
  • log()
  • cls()
  • concatFiles(filesArray)
  • isFile(filePath)
  • isDir(dir)
  • getFilesSync(dir,ext,recurse,includeHidden)
  • writeFile(filePath)
  • promiseWriteFile(filePath)
  • readFileToString(filePath)
  • isArray(input)
  • isObject(a)
  • isEven(n)
  • isOdd(n)
  • roundTo(v,dec)
  • parseObjectToObject(source,target,strict)
  • cutStringTo(s,len)
  • escapeHTML(str)
  • getCleanJSONString(str)
  • getFileExtension(filePath)
  • getFileExtension2(filePath)
  • getLastPartOfUrl(url)
  • getRandomString(len)
  • getStringBetween(str,start,end)
  • getStringsBetween2(str,start,end)
  • inString(needle,haystack)
  • leftTrim(str,noOfChars)
  • rightTrim(str,noOfChars)
  • ucFirst(str)
  • isEmail(email)

Documentation

enableLog()

Information: Enables log command globally.

disableLog()

Information: Disables log command globally.

log()

Information: Shortcut for console.log supports logging in colors.

const aftc = require('aftc-node-tools');
const cls = aftc.cls;
const log = aftc.log;
cls();
log( ('All For The Code ' + 44).green );
log('All For The Code'.red);
log('All For The Code'.green);
log('All For The Code'.blue);
log('All For The Code'.cyan);
log('All For The Code'.yellow);
log('All For The Code'.underline.red);
log('All For The Code'.underline.green);
log('All For The Code'.inverse);
log('All For The Code'.rainbow); 
log('All For The Code'.trap);
log('All For The Code'.trap.bgRed.white);

cls()

Information: Clears the console.

cls();

concatFiles(filesArray)

Information: Concatinates all files in the array into a string.

Parameters:

  • Name: filesArray Type: Array Required: true Info: Array of files.

Returns: String

let files = ['file1.js','file2.js']
concatFiles(arr)

isFile(filePath)

Information: Checks if path is a file.

Parameters:

  • Name: filePath Type: String Required: true Info: Path you want to check is a file.

Returns: Boolean

if ( isFile('./file1.js') ){
	log('Its a file!')
} else {
	log('That aint no file!')
}

isDir(dir)

Information: Checks if path is a directory.

Parameters:

  • Name: dir Type: String Required: true Info: Path you want to check is a directory.

Returns: Boolean

if ( isDir('./mydir') ){
	log('It exists!')
} else {
	log('Nooooo!')
}

getFilesSync(dir,ext,recurse,includeHidden)

Information: Gets an array of files in a directory. Hidden files start with a . (linux style, not windows)

Parameters:

  • Name: dir Type: Array Required: true Default: null Info: Directory.

  • Name: ext Type: String Required: true Default: * Info: Array of files.

  • Name: recurse Type: Boolean Required: false Default: false Info: Array of files.

  • Name: includeHidden Type: Boolean Required: false Default: false Info: Array of files.

Returns: Array

let files = getFilesSync('./src', '.js', true);

writeFile(filePath)

Information: Writes data to a file.

Parameters:

  • Name: filePath Type: String Required: true Info: Path to file you want to write to (will create it if it doesnt exist).

Returns: Promise

let data = 'hello world'
writeFile('./test.txt',data)
.then((res)=>{ console.log('complete')} )

promiseWriteFile(filePath)

Information: Writes data to a file but returns a promise.

Parameters:

  • Name: filePath Type: String Required: true Info: Path to file you want to write to (will create it if it doesnt exist).

Returns: Promise

let data = 'hello world';
writeFile('./test.txt', data)
   .then(() => {
       log('success')
   });
   .catch(() => {
       log('failure')
   });

readFileToString(filePath)

Information: Returns a file as a string.

Parameters:

  • Name: filePath Type: String Required: true Info: Path to file you want read.

Returns: String

let data = readFileToString('./test.txt');

isArray(input)

Information: Detects if the supplied variable is an array or not (instance of returns object).

Parameters:

  • Name: input Type: * Required: true Info: The variable to check.

Returns: Boolean

let varIsArray = isArray(3);

isObject(a)

Information: Detects if the supplied variable is an object or not.

Parameters:

  • Name: a Type: * Required: true Info: The variable to check.

Returns: Boolean

let varIsObj = isObject(3);

isEven(n)

Information: Detects if a number is even or not.

Parameters:

  • Name: n Type: Number Required: true Info: The number you want to check is even.

Returns: Boolean

let answer = isEven(input);

isOdd(n)

Information: Detects if a number is odd or not.

Parameters:

  • Name: n Type: Number Required: true Info: The number you want to check is odd.

Returns: Boolean

let answer = isOdd(input);

roundTo(v,dec)

Information: Rounds a number to a specific amount of decimal places.

Parameters:

  • Name: v Type: Number Required: true Info: The number you want to round.

  • Name: dec Type: Number Required: true Info: The number of decimal places you wish to round to.

Returns: Number

let v = roundTo(3.142,1);

parseObjectToObject(source,target,strict)

Information: Parse an object into another object (good for processing arguments dynamically with strict on).

Parameters:

  • Name: source Type: Object Required: true Info: The source object to pull values from.

  • Name: target Type: Object Required: true Info: The target object to push values into.

  • Name: strict Type: Boolean Required: false Info: To only parse indexes/params that exist in both objects.

Returns: Boolean

let args = {
	a:4
}
parseObjectToObject(arguments[0],args,true);

cutStringTo(s,len)

Information: Returns the string but to the specified length.

Parameters:

  • Name: s Type: Number Required: true Info: The string you want to cut.

  • Name: len Type: Number Required: true Info: The length (number of chars) you want returned.

Returns: String

let answer = cutStringTo(str,5);

escapeHTML(str)

Information: Escapes special characters in a string.

Parameters:

  • Name: str Type: String Required: true Info: The string you want to process.

Returns: String

let newString = escapeHTML(str);

getCleanJSONString(str)

Information: Cleans a JSON string.

Parameters:

  • Name: str Type: String Required: true Info: The string you want to process.

Returns: String

let newJsonString = getCleanJSONString(jsonString);

getFileExtension(filePath)

Information: Gets the extension of the supplied file path string.

Parameters:

  • Name: filePath Type: String Required: true Info: File path string.

Returns: String

let ext = getFileExtension(filePath);

getFileExtension2(filePath)

Information: Gets the extension of the supplied file path string (method 2).

Parameters:

  • Name: filePath Type: String Required: true Info: File path string.

Returns: String

let ext = getFileExtension2(filePath);

getLastPartOfUrl(url)

Information: Gets the last segment of a url.

Parameters:

  • Name: url Type: String Required: true Info: URL string.

Returns: String

let urlLastSeg = getLastPartOfUrl(url);

getRandomString(len)

Information: Returns a string to a specified length of random characters.

Parameters:

  • Name: len Type: Number Required: true Info: The number of random character you want to get.

Returns: String

let randomString = getRandomString(256);

getStringBetween(str,start,end)

Information: Returns a sub string of of a string between specified start and end characters.

Parameters:

  • Name: str Type: String Required: true Info: The source string you want to process.

  • Name: start Type: Number Required: true Info: Start char position.

  • Name: end Type: Number Required: true Info: End char position.

Returns: String

let result = getStringBetween('test test test',5,10);

getStringsBetween2(str,start,end)

Information: Returns a sub string of of a string between specified start and end characters. (method 2)

Parameters:

  • Name: str Type: String Required: true Info: The source string you want to process.

  • Name: start Type: Number Required: true Info: Start char position.

  • Name: end Type: Number Required: true Info: End char position.

Returns: String

let result = getStringBetween('test test test',5,10);

inString(needle,haystack)

Information: Looks for a string inside a string.

Parameters:

  • Name: needle Type: String Required: true Info: String to search for.

  • Name: haystack Type: String Required: true Info: String to search.

Returns: Boolean

let result = getStringBetween('test test test',5,10);

leftTrim(str,noOfChars)

Information: Trims a string from the left.

Parameters:

  • Name: str Type: String Required: true Info: String to trim.

  • Name: noOfChars Type: Number Required: true Info: Number of characters to trim off.

Returns: String

let result = leftTrim('test test test',5);

rightTrim(str,noOfChars)

Information: Trims a string from the right.

Parameters:

  • Name: str Type: String Required: true Info: String to trim.

  • Name: noOfChars Type: Number Required: true Info: Number of characters to trim off.

Returns: String

let result = rightTrim('test test test',5);

ucFirst(str)

Information: Returns a string with the first character uppercase.

Parameters:

  • Name: str Type: String Required: true Info: String to trim.

Returns: String

let UpperFirstString = ucFirst('mooo');

isEmail(email)

Information: Checks if the supplied email is valid or not.

Parameters:

  • Name: email Type: String Required: true Info: Email to validate.

Returns: Boolean

let isValidEmail = isEmail('darcey.lloyd@gmail.com');
0.7.11

3 years ago

0.7.10

3 years ago

0.7.9

3 years ago

0.7.12

3 years ago

0.7.6

3 years ago

0.7.8

3 years ago

0.7.7

3 years ago

0.7.18

3 years ago

0.7.14

3 years ago

0.7.17

3 years ago

0.7.16

3 years ago

0.7.2

3 years ago

0.7.1

3 years ago

0.7.4

3 years ago

0.7.3

3 years ago

0.7.0

3 years ago

0.6.5

4 years ago

0.6.4

4 years ago

0.6.3

4 years ago

0.6.2

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago

0.5.1

4 years ago

0.3.0

4 years ago

0.5.0

4 years ago

0.1.1

5 years ago

0.1.0

5 years ago