Projects

curry.ts

A small & typed jQuery-inspired library with revolutionary async & trigger chaining!

View / Source
Type

library

Active

2021 - 2024

Status

Done

Tags

Library, jQuery, TypeScript

Projects
curry.tslibrary2021 - 2024View / Source

Curry is a simple DOM operations library, inspired by jQuery, one of the most popular JavaScript libraries there is. Or used to be. Its main purpose is to simplify DOM operations, event listeners and adding simple interactivity to the website. The main idea behind this project is to figure out how a library like this is implemented. I did not study how jQuery or other libraries work.

The concept is very similar to the one of jQuery. You create an execution by calling the $() selector function, which exposes the entire instance containing roughly 54 methods.

These methods are divided into several groups


How does it differ?

While the functionality stays pretty true to jQuery, Curry does have a few significant differences.

Async Chains

Method chains are not executed in sync, but are queued. The next link is not run before the previous link finishes running. Not only does this allow triggering events and their results in the same chain, it also allows for things like data fetching or any async operation. The idea is that fewer chains === less code.

// .text() is not run until the click event is fired
$('#trigger').click().text('I\'ve been clicked')

// Run a piece of code, 1 second after all the DOM content loads
$(window).on('DOMContentLoaded').wait(1000).run(() => {
  // ...
})

This functionality also introduces a utility field called .await which is just a promise that resolves once the chain is completed. The syntax is borrowed from Rust.

$(document).on('load', async () => {
  Promise.all([
    $('#first').animate({ opacity: 1 }).await,
    $('#second').animate({ opacity: 1 }, { delay: 300 }).await,
    $('#third').animate({ opacity: 1 }, { delay: 600 }).await,
  ])
    .then(() => {
      // Runs when all fade-in animations complete
    })
})

Types

One of Curry’s main concepts was typing and autocomplete. This should highly reduce the need to repeatedly look up methods and their documentation.

// An example of how the .css() method's types are defined
import type { Properties, PropertiesHyphen } from 'csstype'

interface CSSStyle extends Properties, PropertiesHyphen {}

export type CSS = (
  this: Curry,
  key: keyof CSSStyle | CSSStyle,
  value?: ValueOf<CSSStyle>
) => Curry

Simplicity

jQuery has over the years grown into a massive behemoth. And it makes sense. It was built to solve all the problems with the web. From data fetching, to simple interactivity and so much more. As web standards caught up, most of this functionality became bloat.

Curry mostly focuses on DOM iteration/manipulation, as that can still feel cumbersome at times. Take for instance the following example:

// Native DOM API
const item = document.createElement('p')
item.textContent = 'Hello World'
item.id = 'content'

const parent = document.querySelector('#text-content')
parent.appendChild(item)
// Curry
$('#text-content').appendChild('<p id="content">Hello World</p>')

The native way is not bad, but it can become repetitive if you perform such action multiple times during development.


Footnote

If you’re interested in how this project was made, you can visit the GitHub repository, which includes the source code and full documentation.