Skip to content

Commit

Permalink
Version 1.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Mubarrat committed Dec 27, 2023
1 parent 2110eb7 commit 8d83aaa
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 35 deletions.
86 changes: 75 additions & 11 deletions dist/scripts-loader.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/scripts-loader.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/scripts-loader.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/scripts-loader.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mubarrat/scripts-loader",
"version": "1.0.3",
"version": "1.0.4",
"description": "A lightweight and versatile script loader library for dynamically loading JavaScript files in web applications. Simplify script management, handle dependencies, and ensure smooth integration of external scripts with flexible loading strategies.",
"scripts": {
"build": "tsc && node build.js"
Expand Down
207 changes: 188 additions & 19 deletions src/shortHand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,203 @@
/**
* Define an shortname with objects.
*/
const $ls = Object.assign((data: string | {}[]) => typeof data === "string" ? $ls[detectFormatXmlOrJson(data)](data) : $ls.document(data), {
const $ls = Object.assign((data: string | {}[]) => {

// Define xml with default is document
xml: Object.assign((data: string) => $ls.xml.document(data), {
// Let's check data is a string or an object array
if (typeof data !== "string" && !Array.isArray(data)) {

// Define document function.
document(data: string) { loadScript(validateXmlAsScriptArray(data), "document") },
// Throw an error
throw new Error("`data` should be either string or an object array.");
}

// Define ajax function.
ajax(data: string) { loadScript(validateXmlAsScriptArray(data), "ajax") }
// If data is an string
if (typeof data === "string") {

// Switch case
switch (detectFormatXmlOrJson(data)) {

// If an xml type
case "xml":

// Call Xml Helper Method
$ls.xml(data);
break;

// If an json type
case "json":

// Call Json Helper Method
$ls.json(data);
break;

// If anything else
default:

// Throw an error
throw new Error("Unknown type");
}
}

// Data is an array
else {

// Call the helper method
$ls.document(data);
}
}, {

/**
* An xml helper function.
*/
xml: Object.assign((data: string) => {

// If data isn't string
if (typeof data !== "string") {

// Throw an error
throw new Error("data should be string");
}

// Load script
$ls.xml.document(data);
}, {

/**
* This is document injection rendering mode.
* @param data The xml data.
*/
document(data: string) {

// If data isn't string
if (typeof data !== "string") {

// Throw an error
throw new Error("data should be string");
}

// Load script
loadScript(validateXmlAsScriptArray(data), "document");
},

/**
* This is ajax loading rendering mode.
* @param data The xml data.
*/
ajax(data: string) {

// If data isn't string
if (typeof data !== "string") {

// Throw an error
throw new Error("data should be string");
}

// Load script
loadScript(validateXmlAsScriptArray(data), "ajax");
}
}),

// Define json
json: Object.assign((data: string) => $ls.xml.document(data), {
/**
* An json helper function
*/
json: Object.assign((data: string) => {

// Define document function.
document(data: string) { loadScript(validateJsonAsScriptArray(data), "document") },
// If data isn't string
if (typeof data !== "string") {

// Define ajax function.
ajax(data: string) { loadScript(validateJsonAsScriptArray(data), "ajax") }
// Throw an error
throw new Error("data should be string");
}

// Load script
$ls.json.document(data);
}, {

/**
* This is document injection rendering mode.
* @param data The json data.
*/
document(data: string) {

// If data isn't string
if (typeof data !== "string") {

// Throw an error
throw new Error("data should be string");
}

// Load script
loadScript(validateJsonAsScriptArray(data), "document");
},

/**
* This is ajax loading rendering mode.
* @param data The json data.
*/
ajax(data: string) {

// If data isn't string
if (typeof data !== "string") {

// Throw an error
throw new Error("data should be string");
}

// Load script
loadScript(validateJsonAsScriptArray(data), "ajax");
}
}),

// Define document function.
document(data: {}[]) { loadScript(validateAsScriptArray(data), "document") },
/**
* This is document injection rendering mode.
* @param data The script array.
*/
document(data: {}[]) {

// If data isn't string
if (!Array.isArray(data)) {

// Throw an error
throw new Error("data should be string");
}

// Load script
loadScript(validateAsScriptArray(data), "document");
},

/**
* This is ajax loading rendering mode.
* @param data The script array.
*/
ajax(data: {}[]) {

// If data isn't string
if (!Array.isArray(data)) {

// Throw an error
throw new Error("data should be string");
}

// Load script
loadScript(validateAsScriptArray(data), "ajax");
},

/**
* Load from url
* @param url The url where is data
*/
url(url: string) {

// Create a new request
const xhr = new XMLHttpRequest;

// Open the request
xhr.open("GET", url);

// Define ajax function.
ajax(data: {}[]) { loadScript(validateAsScriptArray(data), "ajax") },
// Handle the request
xhr.onreadystatechange = () => xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200 && $ls(xhr.responseText);

// Define an empty case
""() { throw new Error("Unknown type") }
// Send the request
xhr.send();
}
});

0 comments on commit 8d83aaa

Please sign in to comment.