Micell Documentation

website·Indexed 32 days ago

https://micell.org/

A collection of JavaScript utility functions covering various categories including character, string, date, DOM, cookie, URL, regex, and base64. Provides specialized modules such as emitter for event handling and ua for environment detection.

Tokens
8.3K
Snippets
113
Records
122
Agent score
57%

What's inside Micell

  1. Overview of the Micell utility library

    Micell (pronounced /maɪˈsel/) is a collection of common utility functions designed for daily web development. It provides a set of frequently used functions across projects, along with some uncommon utilities for integrity. It is intended to complement, rather than replace, libraries like Lodash, Momentjs, or Dayjs, and is not meant to be a comprehensive replacement for all utility functions in an application.
  2. Create and use a micell Emitter instance

    Use micell.emitter.create() to instantiate a new Emitter. Each instance provides methods to manage event listeners and trigger events. Alternatively, micell.emitter can be used directly as a global singleton emitter instance.
    // Create a local instance
    const emitter = micell.emitter.create();
    emitter.on('foo', () => console.log('foo'));
    emitter.emit('foo');
    
    // Use the global instance
    micell.emitter.on('bar', () => console.log('bar'));
    micell.emitter.emit('bar');
  3. Install micell

    Install the micell library using npm, yarn, or by including it via a CDN script tag. For CDN usage, jsDelivr and unpkg are supported. You can load the latest version, a specific version, or the ES Modules version.
    # Using npm
    npm i --save micell
    
    # Using yarn
    yarn add micell
    <!-- Latest version via CDN -->
    <script src="https://cdn.jsdelivr.net/npm/micell"></script>
    
    <!-- Specific version via CDN -->
    <script src="https://cdn.jsdelivr.net/npm/micell@0.15.2/dist/micell.js"></script>
    
    <!-- ES Modules version via CDN -->
    <script src="https://cdn.jsdelivr.net/npm/micell@0.15.2/dist/micell.esm.browser.js"></script>
  4. Reduce micell bundle size using babel-plugin-lodash

    To optimize the bundle size and only include the specific micell methods used in your project, use the babel-plugin-lodash plugin. Configure the plugin in your .babelrc file by adding micell to the id array.
    {
      "plugins": [
        ["lodash", { "id": ["micell"] }]
      ]
    }
  5. Basic usage of micell

    Import the micell object to access utility functions such as randomString() for generating random strings and cookie.get() for retrieving cookie values. When loaded via a script tag in a browser, micell is exported as a global micell object.
    import micell from 'micell'
    
    // Generate a random string
    micell.randomString();
    
    // Get a cookie value
    micell.cookie.get('name')
  6. Set a cookie with JSON encoding

    Use setJson(key, value, [options]) to store a value as a JSON string. The value is encoded using JSON.stringify. It supports the same options object as the set method.
    setJson('user', { id: 1, name: 'John' }, { expires: '1M' });
  7. Set a cookie with URI encoding

    Use set(key, value, options) to create or update a cookie. The value is automatically encoded using encodeURIComponent.

    Options:

    • domain (string): The domain the cookie belongs to.
    • path (string): The path the cookie belongs to (defaults to /).
    • expires (number|string|Date): Expiration date. Accepts a Date object, a parsable date string, an integer (representing days), or a numeric string with a unit suffix (Y: year, M: month, D: day, h: hour, m: minute, s: second).
    • max-age (number): Time of existence in seconds.
    • samesite (boolean): SameSite attribute.
    • secure (boolean): Secure attribute.
    set('theme', 'dark', { expires: '7D', secure: true });
  8. Generate RFC4122 UUIDs using uuid

    The uuid function generates a random UUID based on the RFC4122 algorithm.
    uuid();
    // => 'fedb3747-4208-475d-b473-e51b583cddaf'
  9. Conditionally join class names using classNames

    The classNames function generates a space-separated string of class names based on the provided arguments. It supports strings, objects (where truthy values are included), and arrays (which are recursively flattened). This is functionally identical to the JedWatson/classnames library.
    classNames('foo', 'bar'); // => 'foo bar'
    classNames('foo', { bar: true }); // => 'foo bar'
    classNames({ 'foo-bar': true }); // => 'foo-bar'
    classNames({ 'foo-bar': false }); // => ''
    classNames({ foo: true }, { bar: true }); // => 'foo bar'
    classNames('foo', { bar: true, duck: false }, 'baz', { quux: true }); // => 'foo bar baz quux'
    
    const arr = ['b', { c: true, d: false }];
    classNames('a', arr); // => 'a b c'
  10. Generate unique random integers using randomNumbers

    The randomNumbers function returns an array of unique random integers between the specified start (inclusive) and end (inclusive) values.
    randomNumbers(1, 10, 5);
    // => [9, 4, 1, 5, 7]
  11. Perform JSONP requests using jsonp

    The jsonp function performs a JSONP request to a specified URL and returns a Promise.
    jsonp(url, [options])
    
    // Options:
    // callback (string): Callback function name for the response.
    // responseType (string): How to parse the response. Default 'json'.
    // timeout (number): Request timeout. Default 0.
  12. Download files or blobs using download

    The download function triggers a file download from a URL string, a Blob object, or a File object. It allows specifying a custom filename.
    // Download from URL
    download('https://micell.org/logo.svg', 'micell-logo.svg');
    
    // Download a Blob
    const blob = new Blob(['Hello, world!'], { type: 'text/plain;charset=utf-8' });
    download(blob, 'hello-world.txt');