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

jpex-defaults

v2.0.0

Published

Core factories for Jpex

Downloads

7,472

Readme

jpex-defaults

Core factories for Jpex

This package contains a number of injectable factories for Jpex.

Installation

Install via npm

npm install jpex-defaults --save

Usage

Node:

const Jpex = require('jpex');
const defaults = require('jpex-defaults');
Jpex.use(defaults);

// Default factories are now available
const t = Jpex.$resolve('$timeout');

Webpack/Browserify:

var Jpex = require('jpex');
var defaults = require('jpex-defaults');
Jpex.use(defaults);

or...

var Jpex = require('jpex');
var defaults = require('jpex-defaults/dist/jpex-defaults');
Jpex.use(defaults);

HTML/Javascript

<script src="node_modules/jpex/dist/jpex.js"></script>
<script src="node_modules/jpex-defaults/dist/jpex-defaults.js"></script>
<script>
// jpex-defaults is automatically registered when imported via script tags
var t = Jpex.$resolve('$timeout');
</script>

API

$timeout

$timeout(callback, delay)
$timeout is the equivalent of the setTimeout function. The callback function is called after the value of delay has passed. The callback function is only called once.

$interval

$interval(callback, delay)
$interval is the equivalent of the setInterval function. $interval will be called repeatedly at the interval set by delay.

$immediate

$immediate(callback) $immediate calls the callback function on the next available event loop. This is the equivalent of setImmediate in NodeJs or setTimeout(fn, 0) in a browser.

$log

$log(message, [message2, message3...])
This is a wrapper for the console functions

$log('I am console.log');

$log.log

$log.log('I am also console.log');

$log.warn

$log.warn('I am console.warn');

$log.error

$log.error('I am console.error');

$promise

$promise(callback)
This wraps up the native Promise class, without the need for the new keyword. If using $promise in a browser that doesn't support the Promise object, a polyfill must be attached to the window object. The plugin jpex-web (which also includes this package) automatically sets a polyfill if needed.

Calling $promise will create a new Promise with the provided constructor function.
The function takes two parameters: resolve and reject.

return $promise(function(resolve, reject){
  resolve(123);
});

$promise.resolve

Returns a resolved promise.

return $promise.resolve(123);

$promise.reject

Returns a rejected promise.

return $promise.reject().catch(...);

$promise.all

Accepts an array of promises and resolves once all promises have been resolved. If any of the promises is rejected, $promise.all will be rejected.

$promise.all([$promise(...), $promise.resolve(), 123]);

$promise.race

Accepts an array of promises and resolves when any of the promises has been resolved. If any of the promises is rejected, $promise.all will be rejected.

$promise.race([$promise(...), $promise.resolve(), 123]);

$typeof

$typeof(any)
$typeof provides a function that returns the type of any object. The possible return values are:

  • string
  • number
  • boolean
  • function
  • array
  • object
  • date
  • regexp
  • null
  • undefined
var t = $typeof(/a/); // 'regexp'

$copy

$copy(object)
The $copy factory allows you to create a copy any object. It can be used as a function, or as an object which contains shallow and deep copy functions.
The shallow copy will copy an object but will not create copies of its properties. So if an object has a property x which is an object itself, the copied object will have the exact same instance of x.

var obj = { x : [1, 2, 3] };
var shallow = $copy(obj);

$copy.shallow

$copy.shallow(object)

var obj = { x : [1, 2, 3] };
var shallow = $copy.shallow(obj);

$copy.deep

$copy.deep(object)
The deep copy will copy an object and then copy all of that object's properties.

var obj = { x : [1, 2, 3] };
var deep = $copy.deep(obj);

$copy.extend

$copy.extend(target, [obj1, obj2...])
The extend method takes any number of arguments and will deep copy the properties of the last arguments onto the first argument. i.e. $copy.extend(a, b, c) will copy the properties of b onto a and then c onto a. If b and c have the same property, c's property wins.
There are a couple of rules to keep in mind:
If two objects have the same property, and the type of that property is an object, its properties will be combined. i.e. if b.x.foo is set and c.x.bah is set, then a.x will get both properties foo and bah.
If two objects have the same property, and the type of that property is an array, its elements will be overwritten. i.e. if b.x = [1, 2, 3] and c.x = [4, 5] then a.x will be [4,5].

var obj = { x : [1, 2, 3] };
var extended = $copy.extend(0, 1); // 1

extended = $copy.extend({}, obj); // { x : [1,2,3] }

extended = $copy.extend({}, obj, {y : 'why'}); // { x : [1,2,3], y : 'why' }

extended = $copy.extend({}, {x : 'first'}, {x : 'second'}); // { x : 'second' }

extended = $copy.extend({}, {x : [1, 2, 3]}, {x : [4, 5, 6]}); // { x : [1, 2, 3, 4, 5, 6]}