/utyl

An extension to the usual javascript usage

Primary LanguageJavaScript

Utyl api

Utyl is an extension to the usual javascript usage.

Load the Api

We must comprovate if Utyl is loaded, and if it's not charge it.

In NodeJs:

if(typeof Utyl == "undefined"){
  //require needs the Utyl path in your project folder
  // In my case '../source/utyl/utyl.js'
  require("../source/utyl/utyl.js");
}

If you want to use Utyl in the client side is also possible just linking the file, like a normal javascript. To make sure it's working, you can comprovate Utyl existence with the same code.

Components

Trigger

The trigger class works like a boolean but only returns true the first time.

var trigger = new Trigger();

console.log(trigger.get());
console.log(trigger.get());

Result:
-->true
-->false

Triggers can be reseted:

var trigger = new Trigger();

console.log(trigger.get());
console.log(trigger.get());

trigger.reset();
console.log(trigger.get());

Result:
-->true
-->false
-->true

EventMap

So big...I wont write it yet...

Timer

Timers will call asynchrony a function every X ms, and (optional) X times. It's much more precise than setInterval.

//Works with FPS(Frames per second)
var FPS = 10;

new Timer(function(){
  console.log("again...");
}, FPS);

// With x Times should be:

var FPS = 10;
var count = 1000;

new Timer(function(){
  console.log("again...");
}, FPS, count);

If the callback returns true the timer will end (but its optional):

new Timer(function(){
    console.log("hey!");
    return true;
}, 10);

Result:
-->Hey!

Vector2 & Vector3

Vectors contains coordinates. Vector2 for 2D (x,y) and Vector3 for 3D (x,y,z).

Any type of variable can be saved in a vector.

Reference Variable

To use any variable as a reference Utyl contains the Reference Class.

var amount = _(0);
function add(amount){
    amount.value++;
}

console.log(amount.value);

add(amount);

console.log(amount.value);

Result:
-->0
-->1

Inherits

Inherits simplifies the POO inheritance inheritance in javascript.

var Foo = function(){}
Foo.prototype.name = "foo";

var Bar = function(){}
Bar.inherits(Foo); //Parent class is now Foo
Bar.prototype.subname = "bar";

console.log(Bar.name);
console.log(Bar.subname);

Result:
-->foo
-->bar

RGB & RGBA

Rgb and Rgba are classes to use colors simply and fast. Their arguments are similar than in css:

  var rgbColor = new RGB(red, blue, green);
  var rgbaColor = new RGBA(red, blue, green, alpha);

From RGB and RGBA objects you can get the hexadecimal value or the individual values:

  var rgbColor = new RGB(25, 156, 125);
  var rgbaColor = new RGBA(25, 156, 125, 0.5);
  
  //Hexadecimal value from the color (RGB or RGBA)
  console.log(rgbaColor.hex);
  
  //Hexadecimal value from the color (RGB or RGBA)
  console.log(rgbaColor.red);
  console.log(rgbaColor.green);
  console.log(rgbaColor.blue);
  
  //Alpha value from the color(only RGBA)
  console.log(rgbaColor.alpha);

Result:
-->199C7D
-->25
-->156
-->125
-->0.5

String Helpers

Array Helpers

Hash Helpers

Compression

Compressed with Closure Compiler

Want more?

You want more utilities... Say me about them!

Or make it yourself and pull request :D