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

node-and-vite-helpers

v2.1.7

Published

๐ŸŒฑ A personal compilation with helpers for NodeJS and Vite

Downloads

31

Readme

Install

   npm i node-and-vite-helpers

๐Ÿ’ก Helpers

  • selectors

    import { s, sAll, sEl, sElAll } from 'node-and-vite-helpers';
    // import { s, sAll, sEl, sElAll } from 'node-and-vite-helpers/selectors';
    
    s('#id');                     // document.querySelector('#id');
    sAll('.class');               // document.querySelectorAll('.class');
    sEl(element, '.child');       // element.querySelector('.child');
    sElAll(element, '.childs');   // element.querySelectorAll('.childs');
  • setTime

    import { setTime } from 'node-and-vite-helpers';
    // import { setTime } from 'node-and-vite-helpers/set-time';
    
    setTime(1000);    // 1000
    setTime('1000');  // 1000
    
    setTime('1s');    // 1000
    setTime('1m');    // 60000
    setTime('1h');    // 3600000
    setTime('1d');    // 86400000
  • doClass

    import { cx } from 'node-and-vite-helpers';
    // import { cx } from 'node-and-vite-helpers/do-class';
    
    cx('c1', 'c2 c3');            // 'c1 c2 c3'
    cx(validCond && 'active');    // 'active'
    cx(invalidCond && 'active');  // ''
  • head

    import { head } from 'node-and-vite-helpers';
    // import * as head from 'node-and-vite-helpers/head';
    
    import favicon from '../favicon.svg';
    
    /**
     * These functions check if an element already exists in head
     * If exists, update element atribute, otherwise creates the element in head
     **/
    head.title('Home');
    head.meta('theme-color', '#6c46bf');
    head.link('canonical', 'https://site.com/');
    head.favicon(favicon);
    head.faviconBase64('data:image/png;base64,iVBO0KGN...ErkJg==');
    /**
     * This function creates any custom element in head
     * Interesting to use for advanced properties, SEO, etc.
     **/
    
    const gtag = 'XXXXXXXXXX';
    
    head.createElement({
       element: 'script',
       attributes: [
          {
             name: 'src',
             value: `https://www.googletagmanager.com/gtag/js?id=G-${gtag}`,
          },
          {
             name: 'async',
          },
       ],
    });
    
    head.createElement({
       element: 'script',
       textContext: `
          window.dataLayer = window.dataLayer || []
          function gtag() {
             dataLayer.push(arguments)
          }
          gtag('js', new Date())
          gtag('config', 'G-${gtag}')
       `,
    });
  • dates

    import { dates } from 'node-and-vite-helpers';
    // import * as dates from 'node-and-vite-helpers/dates';
    
    // defaults ๐Ÿ”“
    dates.set.locale('pt-BR');
    dates.set.timeZone('America/Sao_Paulo');
    // Preparing examples ๐Ÿคน
    const dateA = new Date('2023-01-02T05:06:42.041Z');
    const dateB = new Date('2023-01-04T04:10:35.208Z');
    const christmasEve = new Date('2023-12-24 00:00');
    /**
     * @return string
     */
    
    dates.toYodaString(dateA);
    // 2023-01-02 02:06:42
    
    dates.toLocaleString(dateA);
    // 02/01/2023 02:06:42
    /**
     * @return Date
     */
    
    dates.toLocaleDate(dateA);
    // 2023-01-02T02:06:42.000Z
    
    dates.pastDate(dateA, 1);
    // 2023-01-01T02:06:42.000Z
    
    dates.futureDate(dateA, 1);
    // 2023-01-03T02:06:42.000Z
    
    dates.getBusinessDate(christmasEve, 1);
    // 2023-12-26T00:00:00.000Z
    
    dates.getBusinessDate(christmasEve, 2));
    // 2023-12-27T00:00:00.000Z
    /**
     * @return object
     */
    
    dates.parse(dateA);
    // { year: 2023, month: 1, day: 2, hours: 2, minutes: 6, seconds: 42 }
    
    dates.diff(dateA, dateB);
    // { situation: 'remaining', years: 0, months: 0, days: 1, hours: 23, minutes: 3, seconds: 53 }
    /**
     * @return boolean
     */
    
    dates.isHoliday(date);
    dates.isWeekend(date);
    dates.isWeek(date);
    dates.isBusinessDay(date);
    dates.isEqual(date, compareDate);
    dates.isSmaller(date, compareDate);
    dates.isBigger(date, compareDate);
    dates.isSmallerOrEqual(date, compareDate);
    dates.isBiggerOrEqual(date, compareDate);
    dates.isBetween(startDate, date, endDate);
  • You can set custom holidays:

    const holidays = {
       1: [ 1, 2 ],
       // ...
       12: [ 25, 31 ],
    };
    
    dates.set.holidays(holidays);
  • You can customize timeZone by overwriting the default params:

    dates.toYodaString(dateA, { timeZone: 'UTC' }));
    // 2023-01-02 05:06:42
    
    dates.toLocaleString(dateA, { locale: 'en-US', timeZone: 'America/New_York' }));
    // 1/2/2023, 12:06:42 AM
  • Both locale and timeZone options have typing suggestions ๐Ÿ“

  • This module assumes that you know the time zone from origin dates ๐ŸŽ“

  • input

    import { striptags, entities, xss } from 'node-and-vite-helpers';
    // import { striptags, entities, xss } from 'node-and-vite-helpers/input';
    striptags('<div>๐Ÿค”...</div>'); // ๐Ÿค”...
    
    xss('<div>๐Ÿค”...</div>'); // &#129300;...
    xss('&lt;div&gt;&#129300;&lt;/div&gt;'); // &#129300;...
    
    entities.decode('&#129300;...'); // ๐Ÿค”...
    // I: Trying broke decode xss ๐Ÿ˜ˆ //
    
    const input = '&lt;div&gt;๐Ÿ‘ฎ&lt;/div&gt;';
    const filteredInput = xss(input);
    
    entities.decode(filteredInput); // ๐Ÿ‘ฎ
    // II: Trying broke decode xss ๐Ÿ‘ฟ //
    
    const input = '&amp;lt;div&amp;gt;๐Ÿ‘ฎ&amp;lt;/div&amp;gt;';
    const filteredInput = xss(input);
    
    entities.decode(filteredInput); // ๐Ÿ‘ฎ
    // Unsafe!
    
    entities.encode('<div>๐Ÿค”...</div>');
    // &lt;div&gt;&#129300;...&lt;/div&gt;
    // โŒ Be careful, consider using xss(string)
    
    entities.decode('&lt;div&gt;๐Ÿค”.../div&gt;', false);
    // <div>๐Ÿค”...</div>
    // โ—๏ธ Be careful, consider using entities.decode(string);

    ๐Ÿ‘ฎ๐Ÿปโ€โ™‚๏ธ Use carefully:

    • The decoding depth of the xss() goes up to two stages.
    • The decoding for display entities.decode() has one stage and re-run striptags before returning the result.
    • This means that even if someone insert more layers in a xss attack, it will display the xss content as text and not execute it.
  • tokenGenerate

    import { tokenGenerate } from 'node-and-vite-helpers';
    // import { tokenGenerate } from 'node-and-vite-helpers/token-generate';
    
    tokenGenerate(8); // '45832c3f', 'fa3fe988', '749ecfaa', ...
  • empty

    import { isEmpty, notEmpty } from 'node-and-vite-helpers';
    // import { isEmpty, notEmpty } from 'node-and-vite-helpers/empty';
    
    isEmpty('');            // true
    isEmpty('   ');         // true
    isEmpty('anything');    // false
    
    notEmpty('');           // false
    notEmpty('   ');        // false
    notEmpty('anything');   // true
  • forceArray

    import { forceArray } from 'node-and-vite-helpers';
    // import { forceArray } from 'node-and-vite-helpers/force-array';
    
    forceArray('string');         // [ 'string' ]
    forceArray(1);                // [ 1 ]
    forceArray(true);             // [ true ]
    forceArray(false);            // [ false ]
    forceArray({});               // [ {} ]
    forceArray(/* any */);        // [ /* any */ ]
    
    forceArray([ /* items */ ]);  // [ /* items */ ]

Credits

| Contributors | GitHub | | ------------ | ---------------------------------------------------------------------------------- | | Author | wellwelwel |