Socket
Socket
Sign inDemoInstall

araz

Package Overview
Dependencies
Maintainers
1
Versions
45
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

araz - npm Package Compare versions

Comparing version 1.1.0 to 1.1.1

6

package.json
{
"name": "araz",
"version": "1.1.0",
"version": "1.1.1",
"description": "",

@@ -26,7 +26,7 @@ "main": "index.ts",

"dependencies": {
"lodash-es": "^4.17.21"
"lodash-es": "^4.17.21",
"@types/lodash-es": "^4.17.8"
},
"devDependencies": {
"@types/lodash-es": "^4.17.8"
}
}

@@ -1,96 +0,86 @@

import { Element } from './types';
import { EventHandler ,Children, ElementAttributes ,ElementCreator } from './types';
import { forEach, isFunction, map, isString, isElement, includes, entries, camelCase, debounce, pickBy, isEmpty } from 'lodash-es';
import { forEach, isArray, startsWith, toLower } from "lodash-es";
export const onBeforeMount = (callback: () => void) => {
const debouncedCallback = debounce(callback, 300);
window.addEventListener("load", debouncedCallback);
};
const addEventAttribute = (
element: HTMLElement,
attributeName: string,
attributeValue: any
) => {
const eventName = toLower(attributeName.substring(2));
element.addEventListener(eventName, attributeValue);
export const onMounted = (callback: () => void) => {
const debouncedCallback = debounce(callback, 300);
window.addEventListener("DOMContentLoaded", debouncedCallback);
};
const addRegularAttribute = (
element: HTMLElement,
attributeName: string,
attributeValue: any
) => {
element.setAttribute(attributeName, attributeValue);
export const onUpdated = (callback: () => void) => {
const debouncedCallback = debounce(callback, 300);
document.addEventListener("DOMContentLoaded", debouncedCallback);
};
const addAttributes = <P extends { [key: string]: any }>(
element: HTMLElement,
props: P
) => {
forEach(props, (value, prop) => {
if (startsWith(prop, "on")) {
addEventAttribute(element, prop, value);
} else {
addRegularAttribute(element, prop, value);
}
});
export const onDestroy = (callback: () => void) => {
window.addEventListener("unload", callback);
};
const appendNode = (parent: HTMLElement, node: Node) => {
parent.appendChild(node);
if (document.readyState == 'loading') {
console.log(document.readyState);
}
const applyStyles = (element: HTMLElement, styles: { [key: string]: string }): void => {
map(styles, (value, key) => {
element.style.setProperty(key, value);
});
};
const appendText = (parent: HTMLElement, text: string) => {
parent.appendChild(document.createTextNode(text));
const addClasses = (element: HTMLElement, classList: string[]): void => {
forEach(classList, className => {
if (!includes(element.classList, className)) {
element.classList.add(className);
}
});
}
const addEventListeners = (element: HTMLElement, events: { [key: string]: EventHandler }): void => {
forEach(entries(events), ([eventName, eventHandler]) => {
const formattedEventName = camelCase(eventName.slice(2));
if (isFunction(eventHandler)) {
element.addEventListener(formattedEventName, eventHandler);
}
});
};
const addChildren = (
element: HTMLElement,
children: Node | string | (Node | string)[] | null
) => {
if (children !== null && children !== undefined) {
const childArray = isArray(children) ? children : [children];
forEach(childArray, (child) => {
if (child instanceof Node) {
appendNode(element, child);
} else {
appendText(element, children.toString());
}
const appendChildren = (element: HTMLElement, children: Children): void => {
forEach(children, child => {
if (isString(child)) {
element.appendChild(document.createTextNode(child));
} else if (isElement(child)) {
element.appendChild(child);
}
});
}
};
export const r = ({ tag, attrs, children }: ElementCreator): HTMLElement => {
const element = document.createElement(tag);
const h = <P extends { [key: string]: any }>({
name,
props = {} as P,
children = null,
setup,
}: Element<P>): HTMLElement => {
const element: HTMLElement = document.createElement(name);
const validStyles = pickBy(attrs.style, isString);
addAttributes(element, props);
if (!isEmpty(validStyles)) {
applyStyles(element, validStyles);
}
addChildren(element, children);
if (attrs.classList && attrs.classList.length > 0) {
addClasses(element, attrs.classList);
}
const beforeOnMounted = (callback: () => void) => {
window.addEventListener("DOMContentLoaded", () => {
callback();
});
};
if (attrs) {
addEventListeners(element, attrs);
}
const onMounted = (callback: () => void) => {
window.addEventListener("load", () => {
callback();
});
};
if (children) {
appendChildren(element, children);
}
if (setup) {
setup({
beforeOnMounted,
onMounted,
});
}
return element;
return element;
};
export default h;
// types.ts
type Setup = ({ beforeOnMounted, onMounted }: { beforeOnMounted?: any; onMounted?: any }) => void;
export type ElementAttributes = {
style: Partial<CSSStyleDeclaration>;
classList?: string[];
[key: string]: any;
};
export interface Element<P extends { [key: string]: any } = {}> {
name: string;
props?: P;
children?: Node | string | (Node | string)[] | null;
setup?: Setup;
}
export type EventHandler = (event: Event) => void;
export type Children = Array<string | Element>;
export type ElementCreator = {
tag: string;
attrs: ElementAttributes;
children: Children;
};
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc