npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

global-js-functions

v0.2.3

Published

Some additional functions for node.js and javascript

Downloads

41

Readme

Global JS Functions

npm Libraries.io dependency status for latest release GitHub top language NPM

npm npm

paypal

This module adds some extra global functions to node.js and javascript. The same file can run both server side and client side.

Installation

npm install global-js-functions

Setup

// express

const express = require('express');
const jsFunctions = require('global-js-functions');

const app = express();

// add express static file
jsFunctions('/cdn/functions.js', app); // returns true on success


// another way to add the functions to client side
app.use('/cdn/functions.js', express.static(jsFunctions.filePath));


// basic http

const http = require('http');
const jsFunctions = require('./index')('/cdn/functions.js');

server = http.createServer(function(req, res){
    // send static file on /functions.js url
    if(jsFunctions(req, res)) return;

    // do other stuff
    res.statusCode = 200;
    res.write('Hello, World!');
    return res.end();
});

server.listen(3000);


// other methods

const jsFunctions = require('./index');

// when all parameters left blank
// if express module exists, this will return the express.static(); function with paths filled
// else, this will return a function which returns a reference to the javascript string
jsFunctions();

// other method express example
const jsFunctions = require('./index');

app.use('/cdn/functions.js', jsFunctions());

// other method http example
const jsFunctions = require('./index')();

if('/cdn/functions.js' === url.parse(req.url).pathname){
    res.writeHead(200, {'Content-Type': 'text/javascript'});
    res.write(jsFunctions());
    return res.end();
}
<!-- to use the static functions.js client side -->
<script src="/cdn/functions.js"></script>
<!-- all above js examples return the same static url -->

<!-- or you can load it through jsdelivr cdn (automatically generated by jsdelivr) -->
<!-- npm -->
<script src="https://cdn.jsdelivr.net/npm/global-js-functions/static/functions.min.js"></script>
<!-- github -->
<script src="https://cdn.jsdelivr.net/gh/aspiesoft/global-js-functions/static/functions.min.js"></script>

Usage

forEach(obj || arr || str, function(value, index, array){
    // this function automatically runs Object.keys() on an object, and returns the object key as the index
    // an array works like normal
    // a string, number, or boolean is added as a single index of an array
});

forEachAsync(obj || arr || str, function(value, index, array){
    // same as forEach, except each callback runs async
    // if you set the option {sync: true} than you can await the loop, and it will return when all callbacks are finished
    // if {sync: true}, you can also set {timeout: 1000, checkInterval: 10}
    // timeout option says when the waiting should cancel
    // checkInterval option tells the function how often it should check to see when the loop is done
    // both options are in milliseconds
}, options);

await sleep(ms); // allows you to make an async function sleep for a number of milliseconds


// the waitForValue function can be a very useful one
async function logAsyncGrabbedValue(){
    let result = false;
    myLongProcess().then(data => {
        result = data;
    });
    
    // do some stuff during async operations
    
    // this function will wait for callback to return true
    // we are waiting until result is set by the myLongProcess async function
    await waitForValue(() => result);
    
    // now that we have the result, we can return it
    return result;
}
async function myLongProcess(){
    let result = 'test';
    await sleep(1000);
    return result;
}
// the waitForValue function is also what the forEachAsync function uses when the sync option is true


// returns milliseconds from string time
toTimeMillis('10m'); // 10 minutes
// any number passes back as if already in milliseconds
toTimeMillis(toTimeMillis(toTimeMillis('10m'))); // still 10 minutes
// you can use one of many time values
us || mic = microsecond
ns = nanosecond
ms = millisecond
s = second
m = minute
h = hour
D = Day
M = Month
Y = Year
DE = Decade
CE || C = Century


// toNumber function sets a string to a number, and removes invalid characters
toNumber('10 and letters that get removed');
// toInteger function does the same as toNumber, but also floors the decimal
toInteger('10.5 and letters and decimal removed');

// sNum function returns an 's' string if a number !== 1
sNum(1); // result = '';
sNum(2); // result = 's';
// this is helpful if you want to display a string that shows a time frame
let loops = 3;
while(loops--){
    console.log('This will loop '+loops+' more time'+sNum(loops));
}
// output:
// This will loop 3 more times
// This will loop 2 more times
// This will loop 1 more time

// notice the last "time" does not say "1 more time(s)"


// this function escapes regex by adding a "\" in front of every non alphabetic or numeric character
RegExp.escape(str);

// replaces all occurrences in a string
String.replaceAll(str, search, replace);
// replaces the last occurrence in a string
String.replaceLast(str, search, replace);

// reverses the characters in a string, or by a separator
String.reverse(str, separator);

// fixes json string to proper format
JSON.normalizeStr(`{test1: "1", test2: "test 2",}`); // output: {"test1":1,"test2":"test 2"}

// the math sum operator. Basically a for loop
// answer += callback(n);
let result = Math.sum(1, 5, (n) => 2*n);

// the math factorial operator
Math.factorial(n);


// sets html characters to htmlentities
// also does Not set &amp; to &amp;amp;
HTML.escape(str);
// unsets html characters from htmlentities
HTML.unescape(str);
// strips html tags (unless that tag is mentioned in keep parameter)
// stripParams can be set to true || false to strip params from kept html tags
// stripParams default = true
HTML.strip_tags(str, keep, stripParams);
// removes the parameters from all html tags
HTML.strip_params(str);

// Array has .map and .forEach, so why not put this on Object
Object.map(obj, callback); && Object.forEach(obj, callback);

// sets an object to an array
keepKeys = 'array' || 'string' || false;
Object.toArray(obj, keepKeys);
// keepKeys = 'array' output: [[key, value], [key, value]]
// keepKeys = 'string' output: ['key => value', 'key => value']
// keepKeys = false output: [value, value]

// returns an array as an object
Array.toObject(arr);

// returns a clone of a function
Function.clone(func);

// returns a clone of an object
Object.clone(obj);

// returns a clone of an array
Array.clone(arr);