util-pack

1.0.3 • Public • Published

Installation

Using the npm command

npm i util-pack

Overview

This is a growing library of functions for myself to use when coding other projects, hopefully they can help you too.

Disclaimer

Most of the code used here comes from StackOverflow, some are slightly modified for ease of use

Pull a request in Github if you have any functions that can be added it would be really helpful.

Delay

Note that the freeze() function use a while loop to freeze everything you are running

const util = require('util-pack');

// sleep for 3 seconds then do stuff
var ms = 3000;
util.sleep(ms).then(()=>{
    // do stuff
});

// or use a callback
util.sleep(ms,()=>{
    // do stuff
};

// freeze everything with freeze(ms) (not recommended)
util.freeze(ms);
// do stuff

Objects

The assign() function will modify the original objects

const util = require('util-pack');

var objectToChange = {hello: 'world'};

util.assign(objectToChange, 'test.test1.test2', 'testing');
console.log(objectToChange); // {hello: 'new value',test: {test2: 'testing}}

util.fetch(objectToChange,'test.test1.test2') // -> new value

Logging

When using functions in the category you must create a text file first, like log.txt or it will give you an error

const util = require('util-pack');

// log 'Hello World' to './log.txt'
util.log({
    data: 'Hello World', // the data the log
    path: './log.txt', // the path of the file
    date: true // to include date in the log or not (other choices: false)
});

Better Trim

An enhancement to the default trim() function, works with characters and strings

const util = require('util-pack');

// count number of space in the front of a string
util.countFront('    Hello World ', ' '); // -> 4

// count number of 'rr' in the end of string
util.countEnd('rrr grrrrr', 'rr'); // -> 2

// trim out specified characters or string
util.trim('@Hello@World@@@','@'); // -> 'Hello@World'

Date and Time

Basically the same thing as the Date() thing, but more customizable

If you didn't add the required parameters, it will use the default values

const util = require('util-pack');

// get year (default: {type: 'str', format: 'full'})
util.getYear({
    type: 'num', // other choices: 'str'
    format: 'short' // other choices: 'full'
} // -> 21

// get month (default: {type: 'str', format: 'normal'}
util.getMonth({
    type: 'str', // other choices: 'num'
    format: 'short1' // other choices: 'short2', 'full', 'normal'
} // -> 'Aug'

// 'short1' do 'Sept' and 'short2' do 'Sep'

// get day (default: {type: 'str'})
util.getDay({
    type: 'str' // other choices: 'num'
} // -> '22'

// get hour (default: {type: 'str', format: '24'})
util.getHour({
    type: 'str', // other choices: 'num'
    format: '24' // other choices: '12'
} // -> '03'

// get minute (default: {type: 'str'})
util.getMinute({
    type: 'num'
} // -> 22

// get second (default: {type: 'str'})
util.getSecond({
    type: 'str'
} // -> 22

// get millisecond (default: {type: 'str'})
util.getMs(); // -> '223'

Package Sidebar

Install

npm i util-pack

Weekly Downloads

0

Version

1.0.3

License

MIT

Unpacked Size

11.7 kB

Total Files

10

Last publish

Collaborators

  • siriusmart