🚀 DAY 3 OF LAUNCH WEEK:Announcing Bun and vlt Support in Socket.Learn more →
Socket
Book a DemoInstallSign in
Socket

dangit

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

dangit

A utility library for random JavaScript quirks.

Source
npmnpm
Version
0.1.0
Version published
Weekly downloads
9
28.57%
Maintainers
1
Weekly downloads
 
Created
Source

dangit

Build status for dangit on Codeship.

A utility library for random JavaScript quirks.

  • Type checking
  • Global object retrieval
  • Namespacing
  • API helpers

Version: 0.0.4

Documentation: https://sholladay.github.io/dangit/

Installation

npm install dangit --save

Usage

Retrieve the true type of any input, as a lowercase string.

var input = null;
dangit.whatis(input);  // => "null"

But why type all that and still have to do a strict equality check? Fuggedaboutit.

var input = null;
dangit.isNull(input);  // => true

Congrate, you saved 3 characters over typeof and you weren't lied to.

Another benefit is more intuitive operator precedence.

Next let's get the ever present global object.

dangit.getTheGlobalObject();  // => window in browsers, global in Node.js, etc.

It's a smarty pants function and won't get tricked as easily as you may think.

// anywhere in Node.js
var window = {};
dangit.getTheGlobalObject() === window;  // => false

or...

// anywhere in a browser
var global = {};
dangit.getTheGlobalObject() === global;  // => false

Isn't that warm and cozy? Just look at it.

Another common task is to construct an API namespace. Let's do that.

var ns = dangit.namespace('superb.llamas');  // => returns a namespaced global object

or if you already have something to extend...

var x = { y:{} },
    z = x.y,
    ns = dangit.namespace(z, 'pirates.forever');  // => returns a new object, which is only global if z was

Notes:

  • It is destructive by default in that if any part of the chain is not a truthy object or function, it has to overwrite that property with a new object to keep going. But...
  • If you pass false as the last argument, it will turn off force mode, throwing an error instead. And...
  • It will never destroy anything it doesn't have to.
  • To protect you from yourself, it only ever considers an object's own properties. An API without this restriction would not be difficult, file an issue if you want it.

Once you've got a namespace, you could really use some ninjas to help solve that issue where you want everything to be coerced to an easy-to-process list. Or a unicorn - that would do nicely, too.

function dream() {
    var args = dangit.flatten(arguments);
    console.log(args.join(' ') + ', whatever');
}
dream(['people', ['pass'], ['weird']], 'stuff')  // => "people pass weird stuff, whatever"

Yeah that's basically a real actual, magical unicorn for your APIs.

Who cares if they used querySelector or querySelectorAll, just flatten and process whatever you get the same way, 100% of the time.

So then you've got your cool new 3rd party JavaScript library and you find out your logger doesn't work on some website because they decided to prevent developers from accidentally being noisy in production.

dangit.isNative(console.log);  // => true only if it hasn't been overwritten
console.log = function () {};
dangit.isNative(console.log);  // => false

Note: This only works for functions and does not guarantee their properties are intact, file an issue if you want more.

And if you really flippin' want the console back, we've got some hacks up our sleeve.

dangit.resetConsole()

So now you want to figure out if it makes sense to loop over some input. It could be a NodeList, HTMLCollection, a plain old array, or something far more devious.

var input = 'abc';
dangit.isArrayish(input);  // => false, even though it has a length of 3
input = function (a, b) {};
dangit.isArrayish(input);  // => false, even though it has a length of 2
input = document.querySelectorAll('a');
dangit.isArrayish(input);  // => true, even though it is not a typical array

Then you encounter some ES5 interestingness where properties aren't allowed to be modified. Eventually you discover Object.isFrozen which is supposed to help you plan for this, but it throws errors for half the input in the galaxy.

var input = null;
console.log(typeof input);  // => "object"
Object.isFrozen(undefined)  // => Uncaught TypeError: Object.isFrozen called on non-object
dangit.isFrozen()           // => false

After tooling up to process input, you'll come across situations where you need to provide defaults or keep track of state during asynchronous tasks. Do this by stamping a new object with a bunch of properties.

var keys   = ['a', 'b', 'c', 'd'],
    values = false;
dangit.stampObject(keys, values);  // => {a: false, b: false, c: false, d: false}

You can provide either argument as a simple value or an array-like object, they will be flattened. Values will be used until there's no more left, at which point the last one will become sticky.

var keys   = ['a', ['b', 'c'], 'd'],
    values = [false, true];
dangit.stampObject(keys, values);  // => {a: false, b: true, c: true, d: true}

And also...

var keys   = ['a', 'b', 'c', 'd'],
    values = [false, 1, 1, true, 6, 'moo' ];  // it is safe to over-provide
dangit.stampObject(keys, values);  // => {a: false, b: 1, c: 1, d: true}

Do some stuff with that stamp, then when the time is right, make sure the results were what you expected.

var keys   = ['a', 'b', 'c', 'd'],
    values = false,
    stamp  = dangit.stampObject(keys, values);  // => {a: false, b: false, c: false, d: false}
// ... do async stuff with each, set to true when complete ...
// ... then, later...
dangit.checkStamp(stamp, true);  // => true

Note: Due to the non-guaranteed order of enumerating objects, this is not quite like .stampObject() - it can only take a simple value to check for an entire object. To compensate a bit, it does non-strict equality checking by default, with a third boolean argument for making it strict. File an issue if you want more.

Contributing

See our contribution guidelines for mode details.

  • Fork it.
  • Create your feature branch: git checkout -b my-new-feature
  • Commit your changes: git commit -am 'Add some feature'
  • Push to the branch: git push origin my-new-feature
  • Submit a pull request.

License

MPL-2.0

Go make something, dang it.

Keywords

js

FAQs

Package last updated on 04 Apr 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts