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

swint-helper

v1.2.11

Published

Helper functions and classes for Swint

Downloads

474

Readme

swint-helper

Greenkeeper badge Helper functions and classes for Swint

Installation

$ npm install --save swint-helper

API

.defaultize(defVal, tgtVal)

Filling default value to an JavaScript object.

If tgtVal doesn't have the key -> It is filled from defVal, recursively.

If tgtVal doesn't have enough values at the Array -> It is filed from defVal, recursively.

Examples

var def,
	tgt;

def = {
	a: 1,
	b: 'aaa',
	c: [1, 2, 3]
};

tgt = {
	b: 'bbb',
	c: [4]
};

defaultize(def, tgt);
// tgt ==>  { a: 1, b: 'bbb', c: [4, 2, 3] }

.validate(ruleVal, tgtVal)

Recursively looks through an object to see if its values match the rules

Rules

  • undefined : Can be any type
  • 0 : Has to be a Number
  • '' : Has to be a String
  • 'aaa\bbbb\bccc' : Has to be enum('aaa', 'bbb', 'ccc')
  • new Date(0) : Has to be a Date
  • {} : Has to be an object
  • [0] : Has to be an Array of Numbers
  • [''] : Has to be an Array of Strings
  • [new Date(0)] : Has to be an Array of Dates
  • [[0]] : Has to be an Array of Arrays of Numbers
  • { a: 0 } : Has to be an Object with key of a: Number

Examples

var r = {
		a: 0,
		b: '',
		c: {
			d: [0],
			e: undefined
		}
	},
	t = {
		a: 100,
		b: 'Monday',
		c: {
			d: [1,2,3],
			e: 'Friday'
		}
	};

var o = validate(r, t);
// o ==> [ true, [] ]

If the target didn't match the target, it will return false and a path to the failing entry:

var r = {
		a: 0,
		b: '',
		c: {
			d: [0],
			e: undefined
		}
	},
	t = {
		a: 100,
		b: 'Monday',
		c: {
			d: ['a', 'b', 'c'],
			e: 'Friday'
		}
	};
 
var o = validate(r, t);
// o ==> [ false, [ 'c', 'd', 0 ] ]

.print([level], msg1, [msg2, ...])

Prints messages and associated levels

Levels

  • 0(print.RAW) : RAW
  • 1(print.DEBUG) : DEBUG(default)
  • 2(print.INFO) : INFO
  • 3(print.WARN) : WARNING
  • 4(print.ERROR) : ERROR

Examples

print(0, 'Raw message');
print(print.RAW, 'Raw message');
// ==> Raw message
print(1, 'Debug message');
print(print.DEBUG, 'Debug message');
// ==> DEBUG/2015-05-07T13:55:35.612Z)
//		Debug message
print(2, 'Info message');
print(print.INFO, 'Info message');
// ==> INFO /2015-05-07T13:55:35.612Z)
//		Info message
print(3, 'Warning message');
print(print.WARN, 'Warning message');
// ==> WARN /2015-05-07T13:55:35.612Z)
//		Warning message
print(4, 'Error message');
print(print.ERROR, 'Error message');
// ==> ERROR/2015-05-07T13:55:35.612Z)
//		Error message

// Setting the printLevel sets a minimum requirement for the levels
global.swintVar.printLevel = 3;		
print(0, 'Raw message');		// Would not print
print(1, 'Debug message');		// Would not print
print(2, 'Info message');		// Would not print
print(3, 'Warning message');	// Would print
print(4, 'Error message');		// Would print

.walk(options)

Walks directory and returns the list of file. The dir should be a full path.

Options

The whitelist precedes blacklist.

  • dir : String, default: path.dirname(require.main.filename)
  • ext : String, default: '*'
  • whitelist : function(fullPath), default: pass
  • blacklist : function(fullPath), default: extension, starting with _
  • baseOrderRule : Boolean, default: true, whether using baseOrderRule
  • orderRule : function(a, b), default: ...
  • head : String, default: 'Intro'
  • tail : String, default: 'Outro'

Example

var list = walk({
		dir: path.join(__dirname, 'myDir'),
		ext: 'js'
	});

.concat(fileList)

Concatenating file contents on the fileList and returns string

Example

var str = concat([
		path.join(__dirname, 'aaa.txt'),
		path.join(__dirname, 'bbb.txt'),
		path.join(__dirname, 'ccc.txt')
	]);

.createHash(options)

Creates key-secret SHA-256 hash pair based on the salt. This pair can be verified the check-hash middleware from swint-middleware.

Options

  • key: Number, default: 15
  • secret: Number, default: 25
  • salt: String, default: SwintIsForTwins

Example

var hashPair = createHash({
	salt: 'ItIsSalty'
});

// hashPair.key, hashPair.secret is generated

.traverseWithQuery(struct, query)

Traverses through a structure by following a list of keys/indexs.

Example

	var s  = { a: [1, 2, { b: 'ccc' } ] },
		s2 = { a: { b: 'ccc2' } };
	
	var o  = traverseWithQuery(s, ['a', 2, 'b']),
		o2 = traverseWithQuery(s2, 'a.b');
	
	// o  ==> 'ccc'
	// o2 ==> 'ccc2'