npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@riadh-adrani/domer

v0.0.5

Published

A library to build DOM faster

Downloads

15

Readme

domer CI/CD npm

A library to build DOM faster.

Install

Add to your project using :

npm i @riadh-adrani/domer

Methods

setConfig

Used to set libraries configuration.

function setConfig(config: LibraryConfig): void;

You can reset by calling setConfig() without an object.

element

Create a DOM element.

function element<T = Element>(tag: string, props: CreateElementProps, children: Array<unknown>): T;
  • tag : element tag.
  • props : object containing attributes and events to be added.
  • children : an array of elements which will be appended as children.

to change namespace, use ns with the desired value as prop. default to HTML.

accepts any children: if it is a Node, it will be appended directly, otherwise, a Text node will be created.

applies class transformation by default.

applies event transformation by default.

text()

create a Text node by transforming data to a string.

function text(data?: unknown): Text;

setEventListener

add an event listener to an element.

function setEventListener(
  key: string,
  event: string,
  value: unknown,
  element: Element,
  modifiers?: Array<EventModifier>,
): void;
  • key : a unique identifier of the event.
  • event : event name like click, input ...etc
  • value : usually an event callback. In case of other value, an empty callback will be added instead.
  • element : target element.
  • modifiers : an array of all modifiers to be applied. modifiers will be applied in order.

stores the event's callback inside the element itself in field called __events__ using key, so it can be later used by removeEventListener.

use key to register multiple event listener for the same event, like click1 and click2.

removeEventListener

remove event listener by its key and event name.

function setEventListener(key: string, event: string, element: Element): void;
  • key : a unique identifier of the event.
  • event : event name like click, input ...etc
  • element : target element.

setAttribute

add an attribute to an element.

function setAttribute(attr: string, value: unknown, el: Element): void;

style attribute accept both an object and string.

toggle attributes will be forced depending on the result of Boolean(value).

removeAttribute

removes an attribute from an element.

function setAttribute(attr: string, el: Element): void;

insertNode

insert node within a parent in a given position.

function insertNode(node: Node, parent: Node, position?: number): void;

appends node at the end if position is invalid

insertNode

change node position within its parent.

function changeNodePosition(node: Node, position: number): void;

appends node at the end if position is invalid

removeNode

remove node from the DOM.

function removeNode(node: Node): void;

setText

update Text content.

function setText(data: unknown, node: Text): void;

Helpers

attrToProp

convert an attribute to a DOM property, or camelcase it otherwise.

function attrToProp(attr: string): string;

extractEventDetails

extract event details from a prop name.

function extractEventDetails(
  prop: string,
): { event: string; modifiers: Array<EventModifier> } | false;

update config to control which event prop should be accepted.

Examples

// invalid
extractEventDetails('click') == false;

// react style
extractEventDetails('onClick') == { event: 'click', modifiers: [] };

// svelte style
extractEventDetails('on:click') == { event: 'click', modifiers: [] };

// vue style
extractEventDetails('@click') == { event: 'click', modifiers: [] };

// with modifiers
extractEventDetails('@click-stop-prevent') == { event: 'click', modifiers: ['stop', 'prevent'] };

isClassProp

check if the given prop is a class related prop.

function isClassProp(prop: string): boolean;

update config to control which class prop should be accepted.

Examples

// invalid
isClassProp('click') == false;

// class
isClassProp('class') == true;

// className
isClassProp('className') == true;

// class directive
isClassProp('class:test') == true;

resolveClassProps

resolve class props and return the final class string.

function resolveClassProps(props: Array<{ value: unknown; key: string }>): string;

accepts an array of objects containg the following keys:

  • value : the value of the property. could be a string, an Array<string> or a boolean with class directive.
  • key : the property/attribute key, like class, className or class:*.

the props will be sorted in the order : class > className > class:*.

Types

Namespace

enum Namespace {
  SVG = 'http://www.w3.org/2000/svg',
  HTML = 'http://www.w3.org/1999/xhtml',
  MATH = 'http://www.w3.org/1998/Math/MathML',
}

EventModifiers

const EventModifiers = ['stop', 'prevent', 'self', 'capture', 'once', 'passive'] as const;

CreateElementProps

interface CreateElementProps extends Record<string, unknown> {
  ns?: string;
}

EventModifier

type EventModifier = (typeof EventModifiers)[number];

EventHandler

type EventHandler = (e: Event) => void;

LibraryConfig

interface LibraryConfig {
  events?: {
    wrapper?: (event: Event, callback: EventHandler) => void;
    syntax?: {
      vue?: boolean;
      svelte?: boolean;
      react?: boolean;
    };
  };
  attributes?: {
    class?: {
      directive?: boolean;
      className?: boolean;
    };
  };
}
  • events

    • wrapper : add a wrapper for all inserted event callbacks.
    • syntax
      • vue : allow vue-style event like @click when creating an element. true by default.
      • svelte : allow svelte-style event like on:click when creating an element. true by default.
      • react : allow react-style event like onClick when creating an element. true by default.
  • attributes

    • class
      • directive : allow class directive. a prop with class:test with a value of true will be evaluated to class of value test. true by default.
      • className : allow className value to be appended to the class attribute. true by default.

ElementWithEvents

interface ElementWithEvents extends Element {
  __events__: Record<string, EventHandler>;
}