GTessarini / SQF

Simplified Query Functions to JavaScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

alt tag

Simplified Query Functions to JavaScript

Lightweight JavaScript library (ultracompatible with browser support, old and new versions, and with many others famous libs) with great similarity with clauses and SQL commands that approach and simplify the human understanding turning lots of line of code in simple, literal and pratical words of command.

Functions for more easily dealing with actions related to searching, modifying, creating and manipulating in various ways the big (or even small) Arrays and JSON objects in Javascript dodging the browser version compatibility issues.

DATA DEFINITION FUNCTIONS:

  • Create JSON objects:

sq.create("HOW", "MANY", "STRING", "ARGUMENTS", "TO", "DEFINE", "THE", "KEYS", "YOU" "WANT");

 var users = sq.create("id", "firstName", "lastName", "email", "country", "state", "city", "faxNumber");
 // users: {id:[], firstName:[], lastName:[], email:[], country:[], state:[], city:[]};   
  • Alter the JSON object Adding it a new key:

JSON_OBJECT = sq.alterAdd(JSON_OBJECT, "NEW_KEY");

users = sq.alterAdd(users, "faxNumber");
// users: {id:[], firstName:[], lastName:[], email:[], country:[], state:[], city:[], faxNumber:[]}; 
  • Alter the JSON object Dropping a specific owned key:

sq.alterDrop(JSON_OBJECT, "KEY_TO_BE_DROPPED");

sq.alterDrop(users, "faxNumber");
// users: {id:[], firstName:[], lastName:[], email:[], country:[], state:[], city:[]};
  • Truncate the JSON object, removing all his data but keeping the structure and definitions:

sq.truncate(JSON_OBJECT);

var dogs = {id:[1,2,3,4,5...9999], name:["A","B","C"..."ZZZZ"], blabla:[001,002...999], ...};
sq.truncate(dogs)
// dogs: {id:[], name:[], blabla:[], ...};

DATA MANIPULATION FUNCTIONS:

  • Insert data into JSON objects/Arrays:

sq.insert(JSON_OBJECT/ARRAY_VALUES, VALUES_IN_ORDER_OF_YOUR_OBJECT_KEYS);

sq.insert(users, 100, "John", "Doe", "e@email.com", "Brazil", "SP", "São Paulo");
// users: {id: [100], firstName: ["Joh"], lastName: ["Doe"], email: ["e@email.com"], country: ["Brazil"], state: ["SP"], city: ["São Paulo"]};

sq.insert(users, 200, "Joan", "Doe", "e2@email.com", "Brazil", "RJ", "Rio de Janeiro");
// users: {id: [100,200], firstName: ["John", "Joan"], lastName: ["Doe","Doe"], email: ["e@email.com", "e2@email.com"], country: ["Brazil", "Brazil"], state: ["SP","RJ"], city: ["São Paulo","Rio de Janeiro"]};
    var myArray=[], myArray2=[], myArray3=[];
    1.  sq.insert(myArray, 1,2,3,4,5,6,7,8,9,0,"A","B","C");
    2.  sq.insert(myArray2, [1,2,3,4,5,6,7,8,9,0,"A","B","C"]);
    3.  sq.insert(myArray3, [1,2,3,4,5,6,7,8,9,0],["A","B","C"]);
/*     1.  myArray: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, "A", "B", "C"];
       2.  myArray2: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, "A", "B", "C"];
       3.  myArray3: [[1, 2, 3, 4, 5, 6, 7, 8, 9, 0], ["A", "B", "C"];   */
  • Update data present in array of values:

sq.update(ARRAY_VALUES, VALUE_TO_BE_UPDATED, CHANGE_TO_BE_DONE);

// users: {id: [100,200], firstName: ["John", "Joan"], lastName: ["Doe","Doe"], email: ["e@email.com", "e2@email.com"], country: ["Brazil", "Brazil"], state: ["SP","RJ"], city: ["São Paulo","Rio de Janeiro"]};
sq.update(users.lastName, "Joan", "Jannet");
// users: {id: [100,200], firstName: ["John", "Jannet"], lastName: ["Doe","Doe"], email: ["e@email.com", "e2@email.com"], country: ["Brazil", "Brazil"], state: ["SP","RJ"], city: ["São Paulo","Rio de Janeiro"]}; 
  • Delete data present in Array of values:

ARRAY_VALUES = sq.delete(ARRAY_VALUES, VALUE_TO_BE_DELETED);

users.city = sq.delete(users.city, "Rio de Janeiro");
// users: {id: [100,200], firstName: ["John", "Jannet"], lastName: ["Doe","Doe"], email: ["e@email.com", "e2@email.com"], country: ["Brazil", "Brazil"], state: ["SP","RJ"], city: ["São Paulo"]}; 
var myArray = [1,2,3,4,5,6,7,"Ops"];
myArray = sq.delete(myArray, "Ops");
//myArray = [1,2,3,4,5,6,7];

DATA VALIDATION FUNCTIONS:

  • Where condition is valid in a array of values, the data required or his position into the array is returned in the desired quantity when specified:

sq.where(ARRAY_VALUES, "CONDITION_OPERATOR", CONDITION_VALUE, [IS_THE_RESULT_POSITION], [QUANTITY])

When the condition is not valid inside he values, null is returned.

Acceptable comparisions: "==","===","!=","!==",">",<",">=","<=","isNull","isNotNull","isEven","isOdd","typeof","instanceof".

var myArray = [1,2,3,4,5,6,7,8,9,10, true, "true", false, null, "Jonathan"];
  1.  sq.where(myArray, ">", 5);            //Values that are all greater than 5
  2.  sq.where(myArray, ">", 5, true);      //Position (index) of all values greater than 5
  3.  sq.where(myArray, ">", 5, true, 2);   //Position (index) of just 2 values greater than 5
  4.  sq.where(myArray, "!==", "true");     //Values that are all different in value and type from the String "true";
  5.  sq.where(myArray, "===", null, true, 9999);  //Position (index) of no later than 9999 values that are equal to null;
  6.  sq.where(myArray, "===", "Jonathan", true);  //Position (index) of the value that is equal to "Jonathan" (in value and type) 
                   
/*     1.  [6, 7, 8, 9, 10];
       2.  [5, 6, 7, 8, 9];
       3.  [5, 6];
       4.  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, true, false, null, "Jonathan"];
       5.  13;
       6.  14;    */
  • Difference between two arrays (in length or in values):

sq.difference(ARRAY_VALUES_1, ARRAY_VALUES_2, [IS_IN_LENGTH]);

var myArray = [1, 2, 3, 4, 5, "A", "B", "C"], myArray2 = [1,2, 3, "A", "B", "C", "D"];
sq.difference(myArray, myArray2);
// --> [4, 5, "D"]
var myArray = [1, 2, 3, 4, 5], myArray2 = [1, 2, 3];
sq.difference(myArray, myArray2, true);
// --> 2;
  • Is Unique value among all others inside array (without repetition):

sq.isUnique(VALUE, ARRAY_VALUES);

var myArray = [1, 2, 2, 3, 4, 5];
1.  sq.isUnique(2, myArray);
2.  sq.isUnique(3, myArray);
/*     1.   false;
       2.   true;   */
  • Is Null value:

sq.isNull(VALUE);

var myArray = [1, 2, null, "null", 5];
1.  sq.isNull(myArray[0]);
2.  sq.isNull(myArray[1]);
3.  sq.isNull(myArray[2]);
4.  sq.isNull(myArray[3]);
/*     1.  false;
       2.  false;
       3.  true;
       4.  false;    */
  • Is Not Null value:

sq.isNotNull(VALUE);

var myArray = [1, 2, null, "null", 5];
1.  sq.isNotNull(myArray[0]);
2.  sq.isNotNull(myArray[1]);
3.  sq.isNotNull(myArray[2]);
4.  sq.isNotNull(myArray[3]);
/*     1.  true;
       2.  true;
       3.  false;
       4.  true;    */
  • Is Even value:

sq.isEven(VALUE);

1.  sq.isEven(3);
2.  sq.isEven(10);
/*    1.   false;
      2.   true;    */
  • Is Odd value:

sq.isOdd(VALUE);

1.  sq.isOdd(3);
2.  sq.isOdd(10);
/*    1.   true;
      2.   false;    */

DATA CONVERSION FUNCTIONS:

  • Null To specific value:

sq.nullTo(ARRAY_WITH_NULL_VALUES, CONVERSION_VALUE);

ARRAY_INDEX_WITH_NULL_VALUE = sq.nullTo(ARRAY_INDEX_WITH_NULL_VALUE, CONVERSION_VALUE);

var myArray = [1, 2, null, null, 4, 5], myArrayTwo = [1, 2, null, null, 5, 6]
1.  sq.nullTo(myArray, 3);
2.  myArrayTwo[2] = sq.nullTo(myArrayTwo[2], 3);
3.  myArrayTwo[3] = sq.nullTo(myArrayTwo[3], 4);
/*     1.  myArray: [1, 2, 3, 3, 4, 5];
       2.  myArrayTwo: [1, 2, 3, null, 5, 6];
       3.  myArrayTwo: [1, 2, 3, 4, 5, 6];    */

DATA MATHEMATICAL FUNCTIONS:

  • var minValue = sq.min(ARRAY_VALUES);

  • var maxValue = sq.max(ARRAY_VALUES);

  • var sumValues = sq.sum(ARRAY_VALUES);

  • var prdctValues = sq.multiply(ARRAY_VALUES);

  • var variancValues = sq.variance(ARRAY_VALUES);

  • var deviationValues = sq.devation(ARRAY_VALUES);

  • -- REPEAT FUNCTION TO SIMPLIFY THE CODING OF LOOPS --

sq.repeat(FUNCTION, NUMBER_OF_TIMES);

var myArray = [];
1.  sq.repeat(function(){sq.insert(myArray, 1, 2, 3);}, 3);
2.  sq.repeat(function(){alert("SQF IS AWESOME!");}, 3);
/* --> [1, 2, 3, 1, 2, 3, 1, 2, 3];
     ** TRY IT YOURSELF AND ENJOY **   
*/

/Gabriel Tessarini/

About

Simplified Query Functions to JavaScript

License:MIT License


Languages

Language:JavaScript 100.0%