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

@dschulmeis/ls-utils

v1.3.0

Published

lecture-slides.js utilities ===========================

Downloads

40

Readme

lecture-slides.js utilities

Short description

This is a tiny collection of reusable utility functions and classes extracted from lecture-slides.js and mini-tutorial.js

Screenshot

Ha ha, you were not really expecting a screenshot of a utility library, were you? Anyway, here you go. 🙃

Module string_utils

Utility functions to work with multi-line strings.

import StringUtils from "@dschulmeis/ls-utils/string_utils.js";
  • function escapeHTML(html): Replace all occurances of &, <, > with &amp;, &lt;, &gt;.

  • function determineLinebreaks(text): Find out which type of linebreak is used in a multi-line string. Returns a new string with the found linebreal sequence:

    • "\r\n": Windows style
    • "\n": Unix style
    • "\r": Mac style
    • "": No linebreaks found
  • function shiftLinesLeft(text): Shift all lines of a multi-line text as far left as possible without destroying indention. This means, first the minimum amount of leading white space of all lines is searched and then removed from all lines.

  • function removeLeadingLinebreaks(text): Remove all empty lines at the beginning of a multi-line text.

  • function removeTrailingLinebreaks(text): Remove all empty lines at the end of a multi-line text.

  • function trimLines(text): Remove trailing white space at the end of each line in a multi-line text.

  • function removeSurroundingWhitespace(text): Removes all leading and trailing empty lines of a multi-line text and shifts all lines as far left as possible without destroying indention. Useful to gobble empty lines and leading spaces in code examples to keep the HTML niceley foramtted.

Module dom_utils

Browser-side utility functions for DOM manipulation.

import DOMUtils from "@dschulmeis/ls-utils/dom_utils.js";
  • function parseHtml(html): Trivial replacement for jQuery's parseHtml function. Takes a trusted(!) HTML string as input and returns a NodeList of the parsed DOM nodes. Beware, that this could potentialy execute JavaScript code in the context of the current document, if the HTML code is coming from an untrusted source!

Class ObservableValue

import ObservableValue from "@dschulmeis/ls-utils/observable_value.js";

This is a simple utility class which allows to define a simple kind of one-way data-binding for variables and object attributes. Whenever the observable value changes all bound observers will be called. Thanks to proper setter and getter methods, the value can be used like a plain variable to set and get its value, but anytime a new value is set all registered observers will be called:

  • Plain functions, called like func(newValue, oldValue)
  • DOM elements whose innerHTML will be updated (with or without HTML escaping)

Additionaly, validator functions can be attached to check whether a changed value is allowed. If any validator rejects the change, the old value will be kept and no observers will be called.

The class has the following methods:

  • constructor(value): Initlialize new observable with an initial value.

  • get value(): Get the current value.

  • set value(): Attempt to set a new value. First calls all validators to check whether the new value is allowed and then calls all bound functions and updates all bound DOM elements.

  • refresh(): Unconditionaly recall all bound functions and reupdate all bound DOM elemnts.

  • addValidator(func): Add a new validator function to check whether a new value is allowed. The function must be of the following form:

function validatorFunction(oldValue, newValue) {
    return true;  // Change is allowed, otherwise return false
}
  • removeValidator(func): Removes a previously registered validator function. Does nothing if the given function is not found.

  • bindFunction(func): Add a new observer function that will be called anytime the value changes. The function must be of the following form:

function observerFunction(oldValue, newValue) {
    // Do anything you like
}
  • unbindFunction(func): Removes a previously registered observer function. Does nothing if the given function is not found.

  • bindElement(element, escape): Add a new DOM element whose innerHTML will be set to the new value anytime the value changes. If escape is true, reserved HTML characters will be escaped to prevent HTML injection. Otherwise the value may contain HTML code that will be interpreted by the browser.

  • unbindElement(element): Removes a previously registered DOM element. Does nothing if the given element is not found.

Copyright

ls-utils (https://www.github.com/DennisSchulmeister/ls-utils) © 2017 – 2022 Dennis Schulmeister-Zimolong [email protected] Licensed under the 2-Clause BSD License.