jModifier is a JavaScript utility library focused on the ability to easily query and manipulate Objects.
const $ = jModifier.getAlts();
const myElement = $.q("#my-element", {
uls: "ul",
items: "ul li"
});
$.aa(myElement.uls, {
style: {
transform: [16, 0, 24, 0].map(function(val) {
return "translateY(" + val + "px)"
})
}
}, {
interval: 80,
loop: Infinity,
dupe: 4
});
$.aa(myElement.items, {
innerText: ["Hello,", "world", "this", "is", "jModifier!"],
style: {
background: ["#7986CB", "#64B5F6", "#4DB6AC", "#FFD54F", "#FF8A65"],
textIndent: [32, 64, 16, 88, 0].map(function(val) {
return val + "px"
})
}
}, {
loop: Infinity,
interval: 100,
dupe: 5
});
Question | Answer |
---|---|
What's the current version? | v0.1 |
jModifier stable? | Not yet! |
What's the purpose? | Minimal methods with broad uses. |
Is this a replacement for jQuery? | It's not, however it really depends on what you need done (it can be). |
Can I get help? | Feel free to join my Discord server for help or general conversation: https://discord.gg/fHKgFSF |
Most methods in jModifier are categorized and can be utilized under the following paths:
jModifier.dom
jModifier.string
jModifier.object
jModifier.array
jModifier.function
- Download/Copy the jModifier library into a file in your project with the extension
.js
- Reference the library with a script tag:
<script type="text/javascript" src="/path/to/jModifier.js">
Note: There is also an experimental version of jModifier under the /dev
directory.
Method | Jump to | or Fiddle |
---|---|---|
jModifier.getAlts() |
#getalts | x3bh6055 |
jModifier.alts |
#alts | |
jModifier.for() |
#for | cjpo7krj |
jModifier.dom.query() |
#domquery | e65rk4mj |
jModifier.dom.on() |
#domon | 7de3fp35 |
jModifier.dom.create() |
#domcreate | ufqf8ery |
jModifier.dom.createElement() |
#domcreateelement | 4rzam1nc |
jModifier.dom.createElements() |
#domcreateelements | s0z3cbvx |
jModifier.dom.insertInto() |
#dominsertinto | gnr5q6y9 |
jModifier.string.toNestedArray() |
#stringtonestedarray | 7m031cvb |
jModifier.string.splitFromXOutsideY() |
#stringsplitfromxoutsidey | ovs7Lh3u |
jModifier.object.assign() |
#objectassign | 7udvLmyz |
jModifier.object.isType() |
#objectistype | 2jm5jwrs |
jModifier.object.pathValue() |
#objectpathvalue | zct148bu |
jModifier.object.NumStringBool |
#objectnumstringbool | |
jModifier.array.assign() |
#arrayassign | gm58pyns |
jModifier.array.reverse() |
#arrayreverse | p9n36zL0 |
jModifier.array.shuffle() |
#arrayshuffle | nsa4h11x |
jModifier.array.sum() |
#arraysum | o5L8gvjv |
jModifier.array.fill() |
#arrayfill | gbr7s0jv |
jModifier.array.wrap() |
#arraywrap | qmj4c4Lm |
jModifier.array.getIndex() |
#arraygetindex | aeq1ybsf |
jModifier.array.listy |
#arraylisty | |
jModifier.function.argumentEvaluator() |
#functionargumentevaluator | 1hynpfhp |
jModifier.function.getArguments() |
#functiongetarguments | 8v2hLur9 |
Returns Object of specified or default alternate method paths.
jModifier.getAlts(<Object(Method Paths)>)Utilizes:
jModifier.alts
,jModifier.object.pathValue
// Default alternate paths
const $ = jModifier.getAlts();
console.log("Default: ", $);
// Custom alternate paths
const c = jModifier.getAlts({
query: "dom.query",
create: "dom.create"
});
console.log("Custom: ", c);
Default alternate paths for jModifier.getAlts().
jModifier.alts
Similar to the native for loop. Has options argument for more control.
jModifier.for(<Array|Number(Items)>*, <Function(Callback)>*, <Object(Options)>)Utilizes:
jModifier.array.fill
,jModifier.array.reverse
,jModifier.array.shuffle
,jModifier.array.sum
/* Example with number */
jModifier.for(10, function(item, index) {
console.log(item + 1); // Counting down from 10
}, {
reverse: true, // Starts in reverse since we're counting down
interval: 1000, // 1000ms between each execution
done: function() { // Executes after the loop completes
console.log("O");
}
});
/* Example with elements */
const myElements = jModifier.dom.query("li");
jModifier.for(myElements, function(item, index) {
item.style.textIndent = "32px";
}, {
interval: 2000
});
Current Options | Type | Default |
---|---|---|
interval | Number (ms) |
0 |
reverse | Boolean |
false |
shuffle | Boolean |
false |
dupe | Number |
0 |
delay | Number (ms) |
0 |
loop | Number (including Infinity ) |
0 |
done | Function |
null |
You can easily query elements with this one however it works differently depending on how you use it.
jModifier.dom.query(<Element|Array|String(Target|Parent)>*, <String(Child Target)>, <Boolean(Force Array)>)Utilizes:
jModifier.for
,jModifier.array.wrap
// Simple query
const myElement = jModifier.dom.query("#my-element");
// Query children of element
const childrenUL = jModifier.dom.query(myElement, "ul");
// Query multiple children of multiple elements and return in single Array
const childrenLI = jModifier.dom.query(childrenUL, "li");
// Query children and assign to returning object
const childrenObj = jModifier.dom.query(myElement, {
UL: "ul",
LI: "ul li"
});
Similar to
<Element>.addEventListener
however you can add multiple listeners to multiple elements with one simple function.jModifier.dom.on(<String|Element|Array(Target Element(s))>*, <Function(Callback & Events)>*, <Boolean(useCapture)>)Utilizes:
jModifier.function.getArguments
,jModifier.dom.query
,jModifier.for
// First argument utilizes the .dom.query method therefore you can specify element(s)
// via a string, variable, array, etc.
jModifier.dom.on("#my-element ul li", function(element, mouseenter, click) {
// First argument of the callback function returns an object that consists
// of the elements, target index, and target element
// {elements, index, node}
// Specify the events after the first argument
// (You can add as many events as you like)
// Confused on how to handle multiple events?
// That's super easy with the || operator
let event = mouseenter || click;
let rand = Math.floor(Math.random() * 255);
element.node.style.background = "rgb(" + rand + ", " + rand + ", " + rand + ")";
});
Create multiple elements with a jModifier Nested Array String (
div[ul[li*6]]
).jModifier.dom.create(<String(jModifier Nested Array String)>*)Utilizes:
jModifier.string.toNestedArray
,jModifier.dom.createElements
// The below method will create the reference html
// structure using a jModifier Nested Array String.
const myElement = jModifier.dom.create("div[ul*2[li*3]]");
console.log(myElement);
// You can use other methods of jModifier such as array.assign
// to assign data to the elements once created.
// You can also use Regular Expression Objects. Because why not right?
console.log(jModifier.dom.create(/div*1000/));
Create one or more elements easily.
jModifier.dom.createElement(<String(Tag Name)>*, <Number(Amount)>)
// This one is for creating multiple elements but only of the same tag type.
const div2 = jModifier.dom.createElement("div", 2);
console.log(div2);
// Leave the second argument blank for only 1 element to be created & returned.
const small = jModifier.dom.createElement("small");
console.log(small);
// Note: This method always returns an Array
Create multiple elements with a jModifier Nested Array (
[["div",[["ul",[["li*3"]]]]]]
).jModifier.dom.createElements(<Array(jModifier Nested Array String)>)Utilizes:
jModifier.dom.createElement
// .dom.createElements essentially works exactly the same way as .dom.create
// however it requires a "jModifier Nested Array" (NOT "jModifier Nested Array String")
// This one should be your go-to if you want even better performance.
const myElement = jModifier.dom.createElements([["div",[["ul*2",[["li*3"]]]]]]);
console.log(myElement[0]);
// [["div",[["ul*2",[["li*3"]]]]]]
// the above "jModifier Nested Array" is the
// equivilent to the below "jModifier Nested Array String"
// div[ul*2[li*3]]
Similar to the native
<Element>.insertBefore()
however it works with multiple elements.jModifier.dom.insertInto(<String|Element(Target Parent)>*, <Element|Array(Target Element(s))>*, <Number(Position)>)Utilizes:
jModifier.array.wrap
,jModifier.dom.query
// Creating element to be inserted into the 1st UL
const newLI = document.createElement("li");
newLI.innerText = "Inserted Element #1";
// Inserting element #1
// The first argument utilizes the .dom.query method
jModifier.dom.insertInto("#my-element ul:nth-child(1)", newLI);
// if no position is specified, the element will be appended to the bottom.
// Creating element to be inserted into the 2nd UL
const newLI2 = document.createElement("li");
newLI2.innerText = "Inserted Element #2";
// You can specify a position with the third argument
jModifier.dom.insertInto("#my-element ul:nth-child(2)", newLI2, 1);
// 0 = before 1st item
// 1 = before 2nd item
// etc..
// Note: You can insert multiple elements and specify the position
This is the backbone of the dom element creation but it has many data structure uses.
jModifier.string.toNestedArray(<String(jModifier Nested Array String)>*)Utilizes:
jModifier.string.splitFromXOutsideY
// Alright this one may be a little confusing to understand.
// Bare with me!
// The best way I can explain this is to use element creation as an example.
// When creating multiple, nested elements, you want to specify the parent tag
// and the child tags and the child tags of the child tags.
// So say if we want to create a div, a h1 inside that div,
// then another div a ul inside that with a li inside the ul..
console.log("Example: ",
jModifier.string.toNestedArray("div[h1], div[ul[li]]")
); // output: [["div",[["h1"]]],["div",[["ul",[["li"]]]]]]
// You can use the output of this to create elements even faster with the
// jModifier.dom.createElements() method
/* OUTPUT Explanation below */
// The way this works is that the jModifier Nested Array consists of two
// types of Arrays: "Tag Array" and "Child Array"
// The "Tag Array" holds 2 items: [tag, childArray]
// The "Child Array" holds infinite Tag Arrays: [ tagArray, tagArray, tagArray ]
// It starts with a Child Array
const nestedArray = [ // Start of Child Array
[ // Start of Tag Array
"Tag Array without children"
],
[ // Start of Tag Array
"Tag Array with children", [ // Start of Child Array
[ // Start of Tag Array
"Another tag array without children"
]
]
]
];
// Same example below as above (Might be easier to understand):
// [ [ "T1" ], [ "T2", [ [ "C1" ] ] ] ]
// ^ ^ ^ ^ ^
// ^ ^ ^ ^ Start of Tag Array
// ^ ^ ^ ^
// ^ ^ ^ Start of Child Array
// ^ ^ ^
// ^ ^ Start of Tag Array
// ^ ^
// ^ Start of Tag Array
// ^
// Start of Child Array
// Disclaimer: "jModifier Nested Array" is just a name for convienient reference. I do not claim to be the first person to create this type of data structure.
Splits from x but not if x is within y.
jModifier.string.splitFromXOutsideY(<String(Target)>*, <String(X)>*, <String(Y)>*, <Boolean(removeWhitespace)>)
// Similar to the native <String>.split() method
// however you can control where the split happens.
// (Specifically where the split does not happen)
// So for example if you wanted to split the below
// string by commas but not within the parentheses
const str = "ONE, TWO, THREE (1, 2, 3)";
// Your first argument would be your string, then the comma.
// Then for the third argument, you add the starting character
// and the ending character in the same string.
console.log(
jModifier.string.splitFromXOutsideY(str, ",", "()")
); // output: ["ONE", " TWO", " THREE (1, 2, 3)"]
// If the fourth argument is true, the whitespace will be removed before returning
console.log(
jModifier.string.splitFromXOutsideY(str, ",", "()", true)
); // output: ["ONE", "TWO", "THREE(1,2,3)"]
Object assignment. Similar to the native mehod
Object.assign()
.jModifier.object.assign(<Object(Target)>*, <Object(Assignment)>*)Utilizes:
jModifier.object.NumStringBool
,jModifier.array.listy
const myElement = jModifier.dom.query("#my-element");
// This method works similar to the native Object.assign() although
// it does not assign in "one hit", it assigns recursively.
jModifier.object.assign(myElement, {
style: {
background: "red"
}
});
// In the above, jModifier gets the style property of myElement
// then recursively assigns to the property.
/* Example with plain Object */
const myObject = {
one: 1,
two: 2,
three: {
four: {
five: 5
}
}
};
jModifier.object.assign(myObject, {
one: 100,
three: {
four: {
five: 500
},
addedKey: "hello, world"
}
});
console.log(
myObject
);
/* output:
{
"one": 100,
"two": 2,
"three": {
"four": {
"five": 500
},
"addedKey": "hello, world"
}
}
*/
Check target if target is of a certain types.
jModifier.object.isType(<Any(Target)>*, <Array(Types)>*)
// This method uses the constructor of the specified first
// argument against each specified item in the 2nd argument Array
console.log("Is Object, String, or function: ",
jModifier.object.isType({}, [Object, String, Function])
); // output: true
console.log("Is String or Object: ",
jModifier.object.isType([], [String, Object])
); // output: false
// If the specified item matches the type of any of the specified items in the Array, it will result in true.
Target and return the value of an Object path.
jModifier.object.pathValue(<Object(Target)>*, <String(Path)>*)
// Pretty simple one
const myObject = {
this: {
is: {
a: "Object!"
}
}
};
console.log("Returns path value: ",
jModifier.object.pathValue(myObject, "this.is.a")
); // output: "Object!"
Simply an Object that consists of
{Number, String, Boolean}
.jModifier.object.NumStringBool
Assignment for Object(s) within Array(s) (Inherits
.for
options).jModifier.array.assign(<Array(Target Array)>*, <Object(Assignment)>*, <Object(Options)>)Utilizes:
jModifier.dom.query
,jModifier.for
,jModifier.array.wrap
,jModifier.object.assign
// .array.assign is basically .for & .object.assign combined.
// Works great with elements:
const listItems = jModifier.dom.query("#my-element ul li");
jModifier.array.assign(listItems, {
innerText: "hello, world"
}, {
delay: 1000
});
/* Property Assignment With $ Functions */
// If a function name within an array assignment is equal to "$"
// the function will evaluate and return with "originalValue, index, and item"
// (Make sure to return inside the $ functions)
jModifier.array.assign(listItems, {
innerText: function $(originalValue, index, item) {
return originalValue + " " + (index + 1)
}
}, {
delay: 2000,
interval: 300
});
/* Property Assignment With Arrays */
// Instead of using $ functions, you can also use Arrays
// The each item in the Array will assign to the relative index
jModifier.array.assign(listItems, {
innerText: ["one", "two", null, "four", "five", null]
// use "null" to skip item assignment
// (however the interval still counts the skipped time)
}, {
delay: 3800,
interval: 300
});
/* Property Assignment With Arrays + Duplication */
// With duplication, the property Array assigns the first item
// to each of the elements then 2nd Array item, 3rd, etc.. (to the dupe number)
jModifier.array.assign(listItems, {
innerText: ["THREE", "TWO", "ONE", "HAPPY NEW YEAR!!"]
}, {
delay: 5000,
interval: 100,
dupe: 4
});
/* Using loop option */
jModifier.array.assign(listItems, {
style: {
textIndent: ["32px", "0px", "64px", "0px"]
}
}, {
delay: 7000,
interval: 30,
dupe: 4,
loop: Infinity
})
Returns given Array backwards.
jModifier.array.reverse(<Array(Target)>*)
const myArray = [1, 2, 3];
console.log(
jModifier.array.reverse(myArray)
); // output: [3, 2, 1]
Returns given Array shuffled.
jModifier.array.shuffle(<Array(Target)>*)
const myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(
jModifier.array.shuffle(myArray)
); // output: (view console on jsfiddle)
Sums up given array to stop point and returns result.
jModifier.array.sum(<Array(Target)>*, <Number(Stop Point)>*)
const myArray = [1, 2, 3];
console.log("Without stop point: ",
jModifier.array.sum(myArray)
); // output: 6
console.log("With stop point: ",
jModifier.array.sum(myArray, 2)
); // output: 3
Fills and returns given Array with specified content.
jModifier.array.fill(<Array(Target)>*, <Any(Content)>*)
// If no content is specified (2nd argument), it will return with
// each item as a number starting from 0
console.log(
jModifier.array.fill(new Array(5))
); // output: [0, 1, 2, 3, 4]
// With content:
console.log(
jModifier.array.fill(new Array(3), "foo")
); // output: ["foo", "foo", "foo"]
// Keep in mind, the above does not assign, only returns!
Wraps given argument in Array if not Array already.
jModifier.array.wrap(<Any(Target)>*)Utilizes: jModifier.array.listy
// .array.wrap is useful for when you don't know whether
// an incoming variable is an Array or not
const notArray = "foo";
const isArray = ["bar"];
console.log(
jModifier.array.wrap(notArray)
); // output: ["foo"]
console.log(
jModifier.array.wrap(isArray)
); // output: ["bar"]
// (Always returns as array)
Gets index of item within given Array.
jModifier.array.getIndex(<Array(Target)>*, <Any(Item)>*)
// Useful for getting the index of an element within an Array
const listItems = jModifier.dom.query("#my-element ul li");
const listItem = jModifier.dom.query("#my-element ul:nth-child(1) li:nth-child(3)");
console.log(
jModifier.array.getIndex(listItems, listItem) // (3rd item)
); // output: 2
Simply an Object that consists of list style type Objects. Add your custom Array to this Object for other jModifier method compatibility.
jModifier.listy
Create a table of functions that get evaluated when executed depending on the manipulation function
jModifier.function.argumentEvaluator(<Object(Table)>*, <Function(Manipulation)>)
// The argumentEvaluator method is a great way
// to avoid using multiple if else statements
const evaluator = jModifier.function.argumentEvaluator({
"1:2:3": function() { // make sure to add the colon to seperate arguments
return "123!";
},
"4:5:6": function() {
return "456!";
}
});
console.log(
evaluator(1, 2, 3)
); // output: "123!"
console.log(
evaluator(4, 5, 6)
); // output: "456!"
/* Example with argument manipulation */
const evaluatorWAM = jModifier.function.argumentEvaluator({
"Number:String:Number": function() {
return "Number String Number!";
},
"Object:Object:Object": function() {
return "3x Object!";
}
}, function(value) {
// The below returns the constructor name which if
// matches a function in the above table,
// it will execute said function
return value.constructor.name
});
console.log(
evaluatorWAM(1, "o", 1)
); // output: "Number String Number!"
console.log(
evaluatorWAM({}, {}, {})
) // output: "3x Object!"
Returns arguments of given Function
jModifier.function.getArguments(<Function(Target)>)
function foo(one, two, three) {}
console.log(
jModifier.function.getArguments(foo)
); // output: ["one", "two", "three"]