EasifyJS
![Build Status](https://travis-ci.org/salexzee/Easify.svg?branch=master)
A small library that makes JavaScript easier to work with.
#Contribute
For more information on how to contribute, view CONTRIBUTE.md
or click here.
If you want a list of contributors in order of thier contribution, view CONTRIBUTORS.md
or click here.
Check out the style guide.
#Installation
To get started, include easify.min.js
in your website. Make sure it's included above your websites JS file. To make sure JS doesn't interfere with the loading of your HTML and CSS, it is suggested that you add your JS files right above your closing body
tag.
Normal download usage
<script src="js/easify.min.js"></script>
<script src="js/app.js"></script>
</body>
</html>
Node usage
In the terminal, run npm install easifyjs
<script src="node_modules/easifyjs/easify.min.js"></script>
<script src="js/app.js"></script>
</body>
</html>
Easify is also accessible to your back-end JS by referring to the global scope:
var e = global.Easify();
You'll know it's included if you look in the console and see:
Easify loaded!
#Documentation
Once included/installed, you will need to initialize an Easify object. There are 2 ways to do this:
var e = Easify();
var e = $E();
Now you can access all of the Easify methods by using dot notation.
Note: If you want to play around with Easify, visit easifyjs.com and open the console. There you can use the already setup e
variable to run any of the Easify methods.
For example, find out if a number is odd or not:
e.odd(5);
e.odd(4);
function odd(num) {
if (num % 2 !== 0) {
return true;
} else {
return false;
}
}
odd(5);
odd(4);
String Methods
cap
Used to capitalize the first letter of a provided string.
e.cap('john');
downcase
Converts all letters in a string to lowercase.
e.downcase('HELLO WORLD');
string
Used to check if the provided value is of type 'string'.
e.string('hello world');
e.string(33);
last
Used to get the last letter of a string.
e.last('hello world');
password
Used to get a random assortment of letters, numbers and special characters for use as a password or anything else
e.password(10);
e.password();
remove
Used to remove a specified amount of random letters from a provided string.
e.remove('hello world', 4);
e.remove('hello world', 4);
removeAll
Used to remove all instances of a specified letter from a provided string.
e.removeAll('hello world', 'l');
randomize
Used to randomly sort the letters in a provided string.
e.randomize('hello world');
e.randomize('hello world');
randomcase
Used to randomlly make letters uppercase or lowercase.
e.randomcase('hello world');
e.randomcase('hello world');
repeat
Used to repeate a provided string, a specified amount of times. Also trims off white space from the beginning and end.
e.repeat('John ', 3);
reverse
Used to reverse the order of a string.
e.reverse('hello world');
trim
Removes any white space from the beginning and end of a string
e.trim(' hello world ');
upcase
Converts all letters in a string to uppercase.
e.upcase('hello world');
format
Evaluates a string literal containing one or more placeholders, returning a result in which the placeholders are replaced with their corresponding values.
e.format('Good {time}, how are {who}?', { time: 'afternoon', who: 'you' });
Only in development version
wrap
Used to wrap a provided string inside of a provided HTML element.
e.wrap('hello world', 'h1');
Array Methods
bridge
Combines 2 arrays and returns 1 array of all values
e.bridge([1,2,3], [4,5,6]);
unify
Combines 2 arrays keeping only unique values
e.unify([1, 2, 3], [2, 3, 4, 5]);
checkTypes
Used to check the types of all values contained in a passed in array
e.checkTypes([{}, [], 'hello', 3, function(){}, true]);
has
Used to check if a specific value is inside of an array
e.has([1,2,3], 4);
e.has([1,2,3], 2);
array
Used to check if passed in value is an array
e.array([]);
e.array({});
e.array('hello');
parlay
Creates a new array out of specified indexes from the provided array
e.parlay(['a','b','c','d','e'], [0,3,4]);
removeItem
Returns a new array without the specified index of the input array
e.removeItem(['a', 'b', 'c'], 1);
search
Takes an array and returns a new array of strings with character sequences that match the provided string.
var food = ['ham', 'potatos', 'spam', 'jam', 'chicken'];
e.search(food, 'am');
shuffle
Returnes a new array with the input array's values shuffled
e.shuffle([1,2,3,4,5]);
Object Methods
clear
Takes in an object and clears all the contents.
var person = {name: 'John', age: 21};
e.clear(person);
person;
combine
Takes 2 object arguments or a single argument which is an array of objects.
Returns a new object which has all keys from input objects. If values conflict, the value from the right most object will take precedence
var person = {name: 'John', age: 21};
var dog = {fur: 'black', eyes: 'brown'};
var cat = {fur: 'white', legs: 4};
e.combine(dog, cat);
e.combine([person, cat, dog]);
drop
Removes the specified methods/properties from the input object
var person = {name: 'John', age: 21, title: 'Mr.'};
e.drop(person, ["title", "age"]);
console.log(person);
isObject
Used to check if passed in value is an object
e.isObject({});
e.isObject([]);
e.isObject(4);
e.isObject(null);
keys
Returns an array containing the keys of the provided object
var obj = {name: 'John Smith', age: 21};
e.keys(obj); // > ["name", "age"]
maintain
Returns a new object with only the specified keys
var person = {name: 'John', age: 21, title: 'Mr.'};
e.maintain(person, ['name', 'age']);
objectPush
Adds a property and a value to an object
var person = {name: 'John', age: 21};
e.objectPush(person, 'title', 'Mr.');
proto
Returns the prototype of the provided object
function Hello(){};
Hello.prototype = {greet: 'Hello world'};
h = new Hello();
e.proto(h);
rename
Changes the name of the specified property name of an object
var person = {firstname: 'John'};
e.rename(person, 'firstname', 'name');
clone
Clones an object and its properties/attributes
var person = {firstname: 'John'};
var person2 = e.clone(person);
size
Returns the amount of keys in the provided object
var obj = {car1: 'Mustang', car2: 'Impala', car3: 'Pento'};
e.size(obj);
toArray
Converts an object into an array of arrays containing the key and value
var person = {firstname: 'John', lastname: 'Doe'};
e.toArray(person);
values
Returns an array of the values from the provided object
var person = {firstname: 'John', lastname: 'Doe'};
e.values(person)
DOM Methods
insertHTML
Used to insert text or HTML into a selected element. Can be used with id or class selectors. If class is selected, a 4th(optional) parameter can be used to specify how many will be affected, starting from the first.
e.insertHTML('id', 'id-name', 'hello world');
e.insertHTML('class', 'class-name', 'hello world');
e.insertHTML('class', 'class-name', 'hello world', 2);
elementFromId
Returns the DOM element with the given ID.
e.elementFromId('heading');
elementsFromClass
Returns an array of DOM elements with the given class
e.elementsFromClass('item');
elementsFromTag
Returns an array of DOM elements of the provided tag name
e.elementsFromTag('div');
elementsFromName
Returns an array of DOM elements with the provided name
e.elementsFromName('city');
Universal Methods
compare
Returns true if 2 arrays or objects are the same
var arr1 = [1,2,3];
var arr2 = [1,2,3];
e.compare(arr1, arr2);
var arr1 = [1,2,3];
var arr2 = [1,2,3,4];
e.compare(arr1, arr2);
var obj1 = {name: 'John', age: 21};
var obj2 = {name: 'John', age: 21};
e.compare(obj1, obj2);
var obj1 = {name: 'John', age: 21};
var obj2 = {name: 'Jane', age: 28};
e.compare(obj1, obj2);
equal
Returns true if both arguments are equal (strict).
e.equal(5, 5);
e.equal(5, '5');
e.equal('hello', 'hello');
notEqual
Returns true if both arguments are not equal (strict).
e.notEqual(5, 5);
e.notEqual(5, '5');
e.notEqual('hello', 'Hello');
similar
Returns true if both arguments are equal (not strict).
e.similar(5, 5);
e.similar(5, '5');
e.similar('hello', 'Hello');
notSimilar
Returns true if both arguments are not equal (not strict).
e.notSimilar(5, 5);
e.notSimilar(5, '5')
e.notSimilar('hello', 'Hello');
truthy
Returns true if input is truthy value.
e.truthy(0);
e.truthy(1);
e.truthy('');
e.truthy('hello');
e.truthy([]);
e.truthy([1, 2, 3]);
e.truthy({});
e.truthy({a: 1, b: 2, c: 3});
e.truthy(function(){});
falsey
Returns true if input is falsey value.
e.falsey(0);
e.falsey(1);
e.falsey('');
e.falsey('hello');
e.falsey([]);
e.falsey([1, 2, 3]);
e.falsey({});
e.falsey({a: 1, b: 2, c: 3});
e.falsey(function(){});
ifTrue
Runs a provided function if the comparison returns true.
e.ifTrue(1 < 2, function(){return '1 is less than 2'});
ifFalse
Runs a provided function if the comparison returns false.
e.ifFalse(1 > 2, function(){return '1 is actually less than 2'});
type
Returns the type of a passed in value.
e.type([]);
e.type({});
e.type('hello');
e.type(3);
e.type(true);
methods
Returns an array of the Easify methods.
e.methods();
methodCount
Returns the amount of methods on EasifyJS.
e.methodCount();
Number Methods
add
Performs addition on 2 or more numbers.
e.add(5, 5);
e.add([5, 5, 5]);
subtract
Performs subtraction on 2 or more numbers.
e.subtract(5, 3);
e.subtract([10, 4, 1]);
multiply
Multiplies 2 or more numbers together.
e.multiply(5, 5);
e.multiply([5, 2, 2]); > 20
divide
Performs division with 2 or more numbers.
e.divide(6, 3);
e.divide([20, 2, 5]);
number
Checks if provided argument is of type "number".
e.number(5);
e.number('5');
e.number(true);
odd
Checks if number is odd.
e.odd(5);
e.odd(4);
even
Checks if number is even.
e.even(5);
e.even(4);
PI
Returns the value of PI.
e.PI();
random
Returns a random whole number from 1 to the specified number.
e.random(5);
e.random(5);
e.random(5);
e.random(0);
between
Returns a random whole number between and including the 2 arguments.
e.between(5, 10);
e.between(10, 5);
e.between(5.5, 5.5);
##Tests
Before submitting a pull request, make sure you run the the tests to make sure nothing broke.
First make sure the dependencies are installed with:
npm install
Once the dependencies are installed, run the following:
npm test
Yep, that's it. Simple right?
To add a new test, open test/test.js
. Easify uses Chai's assert library inside of Mocha. Check out the Chai documentation to learn more about using the assert library. Also check out the Mocha website to learn about how our test.js
file is formatted.
Note: Below is the old way to run tests (DEPRICATED)
Navigate to the tests
directory and open index.html
in your default web browser, then click the "Run Tests" button. You'll see a long list of the tests that were run and whether they passed or failed.
To make sure all the tests were run, look in the console and you'll see something similar to the following:
Tests run!
62 passed - 0 failed
Numbers will be different as more tests are added.
Tests are very simple to implement. Here's the format:
tests.push(
function() {
var text = "Short description that will appear when testing";
runTest(comparison, text);
}
);
Here is an example of a real world test:
tests.push(
function() {
var text = "cap() returns a new string with the first letter capitalized";
runTest(e.cap('testing') === 'Testing', text);
}
);