pickpocket.js

1.0.0 • Public • Published

npm version npm license PRs Welcome dependencies

Pickpocket.js

Simplification at its finest

  • Zero Non Dev Dependencies
  • Syntactically Pleasing
  • Extremely Lightweight
  • Simply Enjoyable
  • Minimalistic
  • Simplified
  • Tested

Installation

NPM

npm install --save-dev pickpocket.js

Yarn

yarn add pickpocket.js --save

Pick Pocket Api

Simplified Wrapper Around Common Data Structure

Any

Determine if there are "any" items in pocket

const pocket = pickpocket({
	name: ['name is a required', 'name must be a string'],
	email: ['email is a required field', 'email must be an email']
});


pocket.any();
true

All

Retrieve all data within pocket

const pocket = pickpocket({
	name: ['name is a required', 'name must be a string'],
	email: ['email is a required field', 'email must be an email']
});


pocket.all();
{
	name: ['name is a required', 'name must be a string'],
	email: ['email is a required field', 'email must be an email']
}
let data = { name: '', email: '' };
let rules = { name: 'required', email: 'email|required' };
let validation = validator(data, rules).validate();

validation.errors().all();

List

Flat list of all pocket field items

const pocket = pickpocket({
	name: ['name is a required', 'name must be a string'],
	email: ['email is a required field', 'email must be an email']
});


pocket.list();
[
	'name is a required', 
	'name must be a string', 
	'email must be an email',
	'email is a required field', 	
]

Set

Set entirety of pocket data

const pocket = pickpocket({
	name: ['name is a required', 'name must be a string'],
	email: ['email is a required field', 'email must be an email']
});

pocket.set({
	name: ['eyy'],
	something: ['ohh']
});

Forget

Forget all pocket data

const pocket = pickpocket({
	name: ['name is a required', 'name must be a string'],
	email: ['email is a required field', 'email must be an email']
});

pocket.forget();
pocket.all(); // {}

Has

Determine if specified field "has" items

const pocket = pickpocket({
	tag: [],
	name: [],
	email: ['email is a required', 'email must be an email'],
});


pocket.has('tag'); // false
pocket.has('name'); // false
pocket.has('email'); // true
pocket.has('something_else'); // false

Get

Get _first_ item from specific field

const pocket = pickpocket({
	tag: [],
	name: [],
	email: ['email is a required', 'email must be an email'],
});
pocket.get('email');

List Field

List items for specific field

const pocket = pickpocket({
	tag: [],
	name: [],
	email: ['email is a required', 'email must be an email'],
});
pocket.list('email');
['email is a required', 'email must be an email']

Add

Add item to specific field

const pocket = pickpocket({
	tag: [],
	name: [],
	email: ['email is a required', 'email must be an email'],
});

pocket.add('tag', 'simplified');
pocket.list('tag'); // ['simplified'];

pocket.add('email', 'extrafied');
pocket.list('email'); // ['email is a required', 'email must be an email', 'extrafied']

Set Field

Set specified field items

const pocket = pickpocket({
	tag: [],
	name: [],
	email: ['email is a required', 'email must be an email'],
});

pocket.set('email', ['set', 'email', 'items', 'list']);

pocket.list('email');
['set', 'email', 'items', 'list']

Forget Field

Forget items on the specified field

const pocket = pickpocket({
	tag: [],
	name: [],
	email: ['email is a required', 'email must be an email'],
});

pocket.forget('email');

pocket.list('email');
[]

Utilization

Pickpocket.js (Simplified)

Example One: Form Error Messages (Organized Per Field)

Common Example

const pickpocket = require('pickpocket.js');

const errors = pickpocket({
	name: ['name is required', 'name is not a string', 'name needs to be cool'],
	email: ['email field must be an email']
});

// errors.all()
// errors.list()
// errors.any()
// errors.add('name', 'message')
// errors.list('email')
// errors.has(field)
// errors.set({})
// errors.set('email', [])
// errors.forget()
// errors.forget('email')
// etc...

Example Two: Human Life Cycle (Organized By Life Moments: Life Moments Have Callback Functions)

Advanced example

const pickpocket = require('pickpocket.js');

const life = pickpocket({
	before: [
		firstBeforeMomentCallback, 
		secondBeforeMomentCallback, 
		thirdBeforeMomentCallback
	],
	passed: [
		firstPassedMomentCallback, 
		secondPassedMomentCallback
	],
	failed: [
		firstFailedMomentCallback
	],
	after: [
		firstAfterMomentCallback, 
		secondAfterMomentCallback
	],
});

life.eventful = (human = {}, moment) => {
	if (life.has(moment)) {
		this.list(moment).forEach(event => event(human));

		life.forget(moment);
	}

};

const moments = ['before', 'during', 'failed', 'passed', 'after'];

/**
 * Add during life moment
 */
life.set('during', [
	person => console.log(`${person.name} is going to kindergarden`),
	person => console.log(`${person.name} was held back in kindergarden`),
	person => { 
		person.age++;

		console.log(`${person.name} had there birthday today`)
	},
	person => console.log(`${person.name} is ${person.age} and still in kindergarden`),
	person => console.log(`${person.name} is going to have one heck of a time during life`),
	// etc...
]);


let human = { age: 0, name: 'zak', birthed: 'April 22 1997' };
moments.forEach(moment => life.eventful(human, moment))


### Contribute

PRs are welcomed to this project.
If you want to improve the pickpocket.js library, add
functionality or improve the docs please feel free to submit a PR.


### License

MIT © [Zachary Horton ~ (Clean Code Studio)](https://github.com/zhorton34/pickpocket.js)

Package Sidebar

Install

npm i pickpocket.js

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

383 kB

Total Files

32

Last publish

Collaborators

  • zhorton999