Giter VIP home page Giter VIP logo

curry.js's Introduction

jCurry (curry.js) v1.1.0

We've all heard it, "If you add jQuery to your resume, don't expect an interview". I'm here to change that, adding jCurry, no matter the job application, guarantees you the CEO position.

Cooking up this project finally made me play less Rocket League and focus on some personal work. So I now proudly present to you, jCurry. Fast & small javascript library for quick web development because let's be real, most of us used the same 10 jQuery functions. Written from scratch and without any reference by yours truly.

Install

Simply clone this repository, which also serves as a starting template. If you prefer things your way, simply copy the minified code in the source folder. Happy coding!

Notice: I love feedback. Please, report any issues or feature requests if you have any. I will literally make any of your wishes happen.

Feature requests

As stated, I will be absolutely delighted if someone has a feature requests and if it's in my powers to implement it. That being said, before you request a feature, check out the Projects tab if it isn't already being considered / in development.


API

You start a chain by using a selector and then chain functions as needed. For example:

$(".list") // Get all elements with the class .list
  .children() // Get its child elements
  .first() // Select the first one
  .text("First list item") // Set its text

Selectors

Method Parameters Summary
$(selector) selectors Uses querySelectorAll and accepts wide variety of parameters. Example
$.get() property (optional) Returns either matched elements or array of selected property of matched elements. Example
$.is() condition Iterates over matched elements and if 1 passes the condition, returns true. Example
$.first() callback (optional) Selects the first element from matched elements. Example
$.last() callback (optional) Selects the last element from matched elements. Example
$.nth()
  • index
  • callback (optional)
Selects element at index from matched elements. Example
$.prev()
  • index (optional)
  • callback (optional)
Selects previous element or previous nth element. Example
$.next()
  • index (optional)
  • callback (optional)
Selects next element or next nth element. Example
$.parent() callback (optional) Selects the element's parent node. Example
$.children() callback (optional) Selects the element's child nodes. Example
$.nthChild()
  • index
  • callback (optional)
Selects element's child node at index. Example

Event binding

Method Parameters Summary
$.on()
  • event
  • callback
  • options (optional)
Binds an event listener to the matched elements. Example
$.click()
  • callback
  • options (optional)
Shorthand for attaching an $.on('click'). Example
$.hover() states or function Powerful shorthand for $(selecor).on('mouseenter') and $(selector).on('mouselave') Example

Style binding

Method Parameters Summary
$.addClass() classList Adds class name or list of class names to the matched elements. Example
$.delClass() classList Removes class name or list of class names from the matched elements. Example
$.toggleClass() classList Toggles between class name or list of class names on the matched elements. Example
$.show() displayValue (optional) Adds display=block or user selected display=displayvalue to the matched elements. Example
$.hide() none Adds display=none to the matched elements. Example
$.toggle() onActive (optional) Toggles between $.show() and $.hide(). Example
$.css()
  • property
  • value
or
styleObject
Adds inline CSS to the matched elements. Example

Animations

Method Parameters Summary
$.slideDown()
  • duration (optional)
  • easing (optional)
Display the matched elements with a sliding motion. Example
$.slideUp()
  • duration (optional)
  • easing (optional)
Hide the matched elements with a sliding motion. Example
$.slideToggle()
  • duration (optional)
  • easing (optional)
Toggles between $.slideDown() and $.slideUp(). Example
$.animate()
  • properties
  • options
Animate matched elements to the added properties. Example
$.fadeIn()
  • to
  • options
Display the matched elements with a fading motion. Example
$.fadeOut()
  • to
  • options
Hide the matched elements with a fading motion. Example
$.fadeToggle()
  • from
  • to
  • options
Toggles between $.fadeIn()and$.fadeOut(). Example

Iteration

Method Parameters Summary
$.each() callback Iterates over matched elements and executes callback on each iteration. Example
$.asyncEach() callback Same as $.each() but each iteration requires calling the next() method. Example
$.filter() callback Filters matched elements by the provided condition. Example

DOM Manipulation

Method Parameters Summary
$.append() callback or templateString Appends a new element to the matched elements. Example
$.prepend() callback or templateString Prepends a new element to the matched elements. Example
$.addChild()
  • callback or templateString
  • append (optional)
Prepends a new child element to the matched elements children. Example
$.text()
  • text
  • location (optional)
Replaces or adds text content to the matched elements. Example
$.attr()
  • property
  • value (optional)
Get the value of an attribute for the first element in the set of matched elements or set one or more attributes for every matched element.. Example
$.del() none Removes matching elements from the DOM. Example

Helpers

Every callback exposes the $util object which contains numerous helper functions. You can find description of each function in this documentation here.

State

Every callback exposes the $state property which is a simple way of sharing data in any and inbetween chains. This could be easily solved with a variable outside of your chains but this solution doesn't pollute your code.

$("li").each(({ self, $state }) => {
  if (!$state.listItems) $state.listItems = []
  $state.listItems.push(self.textContent)
})

$("ul").on("click", ({ $state }) => console.log($state.listItems))

Selector

Selects an array of elements which match the selector. It offers extensive ways to query elements. For full documentation check it out on MDN.

$("button") // Selects every button
$(".my-button") // Selects every element with the 'my-button' class
$("#my-id") // Selects the element with id set to 'my-id'
$("[href]") // Selects every element with the 'href' attribute
$("[title=hello]") // Selects every element with the 'title' attribute with value 'hello'

// You can also retrieve the jCurry's $state and $utils

const state = $("$state")
const utilities = $("$util")

You can also attach the .get() function which returns the matched elements so you can manipulate them outside of the curry scope.

// Returns a HTMLCollection of all button elements
const buttons = $("button").get()

// Returns an array of every input's value
const values = $("input").get("value")

Is

Parameters:

  • condition
  • applier default: every (if condition is an array, set if it should apply to 'some', 'every' or 'none' of the selected elements)

Used in conditions. Accepts the same selector syntax as $(). Returns true if condition satisfies at least one element in matched elements.

if ($("[type=checkbox]").is(":checked")) {
  // At least one checkbox is checked
}

if ($("ul").is(["ul", ".specific-list"], "some")) {
  // Checks if ANY <ul> is, well, <ul> and has '.specific-list' class
}

First

Parameters:

  • callback (optional) exposes:
    • self selected element

Selects the first element in matched elements. Accepts a callback.

$("li").first().text("I am first")

Last

Parameters:

  • callback (optional) exposes:
    • self selected element

Selects the last element in matched elements. Accepts a callback.

$("li").last().text("I am last :(")

Nth

Parameters:

  • index
  • callback (optional) exposes:
    • self selected element
    • index of selected element

Selects the element at the provided index in matched elements. It is 1 indexed, meaning if no index or index 0 is provided, it returns the first element.

$("li").nth(2).text("I am second!")

Prev and Next

Parameters:

  • index (optional)
  • callback (optional) exposes:
    • self selected element
    • index of selected element
    • prev previous selector

Selects the previous / next elements of the matched elements.

<button class="un-rev">Reverse</button>
<p>Hello World</p>
<button class="rev">Reverse</button>
$(".rev").on("click", ({ self }) => {
  $(self).prev().text("dlroW olleH")
})

$(".un-rev").on("click", ({ self }) => {
  $(self).next().text("Hello World")
})

Parent

Parameters:

  • callback (optional) exposes:
    • self parent element
    • child previous selector

Selects parent nodes of each matched elements.

<div class="wrapper">
  <p>I am a special text</p>
</div>
$("p").each(({ self }) => {
  if ($(self).parent().is(".wrapper")) {
    $(self).parent().addClass("wrapper-has-p")
  }
})

Children

Parameters:

  • callback (optional) exposes:
    • self child elements
    • parent parent element

Selects each selected elements child nodes.

const listItems = $("ul").children().get()

Nth child

Parameters:

  • index
  • callback (optional) exposes:
    • self selector element
    • children child elements

Selects the child nodes of the matched elements at provided index.

$("ul").nthChild(2).text("I am second!")

On

Parameters:

  • event
  • callback exposes:
    • e or event
    • self
  • options (optional)
    • event listener options (MDN)

Binds an event listener to matched elements.

$(".wrapper").on("mouseenter", ({ self }) => {
  $(self).css("backgroundColor", "red")
})

Click

Parameters:

  • callback exposes
    • e or event
    • self
  • options (optional)
    • event listener options (MDN)

Shorthand for binding $.on('click', callback) to to matched elements..

$("button").click(({ self }) => console.log(`Clicked ${self.textContent}`))

Hover

Usage #1 Parameters:

  • functions
    • enter callback, executes on mouseenter
    • leave callback, executes on mouseleave
  • options (optional)
    • event listener options (MDN)

Shorthand for binding $.on('mouseenter', callback) and $.on('mouseleave', callback) to matched elements.

$("p").hover({
  enter: ({ self }) => $(self).css("background", "red"),
  leave: ({ self }) => $(self).css("background", "transparent"),
})

Usage #2 parameters:

  • callback

In cases where we only want to bind styles / classes and so on when user enters and reset once user leaves. We can use the function parameter which is the equivalent of only using the enter function.

// This example deems the same result as the previous one
$("p").hover(({ self }) => {
  $(self).css("background", "red")
})

NOTE: This is an experimental technology and the reset to previous state might not fully work. Feedback is appreciated.

Add class

Parameters:

  • classList

Appends class or a class list to the matched elements.

$("span").addClass("color-red")
$("span").addClass("color-red background-blue")
$("span").addClass(["color-red", "font-size-14"])

Delete class

Parameters:

  • classList

Removes class or a class list from the matched elements.

$("span").delClass("color-red")
$("span").delClass("color-red background-blue")
$("span").delClass(["color-red", "font-size-14"])

Toggle class

Parameters:

  • classList

Toggles between the provided class or a class list on the matched elements.

$("button").click(({ self }) => $(self).next().toggleClass("active"))

Show

Parameters:

  • displayValue (optional), default: block

Used for showing hidden elements. Applies the display=displayValue style to every matched element.

$("button").click(() => {
  $(".hidden-wrapper").show("inline-flex")
})

Hide

Used for hiding visible elements. Applies display=none style to every matched element.

$("button").click(() => {
  $(".hidden-wrapper").hide()
})

Toggle

Parameters:

  • displayValue (optional), default: block

Toggles between $.show(displayValue) and $.hide() on every matched element.

$("button").click(() => {
  $(".hidden-wrapper").toggle("inline-flex")
})

CSS

Usage #1 parameters:

  • property CSS property name
  • value CSS property value

Appends inline style to the matched elements.

$("ul").css("listStyle", "none")

Usage #2 parameters:

  • style object of CSS property:value pairs

Appends a style object to the matched elements,

$("ul").css({
  listStyle: "none",
  background: "blue",
})

Slide down

Parameters:

  • duration (optional) default: 500 ms
  • easing (optional) default: ease-in-out

Applies revealing sliding down animation to matched elements.

$("button").click(() => {
  $(".hidden-wrapper").slideDown(250, "linear")
})

Slide up

Parameters:

  • duration (optional) default: 500 ms
  • easing (optional) default: ease-in-out

Applies hiding sliding up animation to matched elements.

$("button").click(() => {
  $(".hidden-wrapper").slideUp(1000)
})

Slide toggle

Parameters:

  • duration (optional) default: 500 ms
  • easing (optional) default: ease-in-out

Toggles between $.slideDown() and $.slideUp() on matched elements.

$("button").click(() => {
  $(".hidden-wrapper").slideToggle(1000)
})

Animate

Usage #1 parameters:

  • properties object of properties to animate (like CSS object)
  • options (optional)
    • length (optional) default: 500 ms
    • easing (optional) default: ease-in-out
    • callback (optional) executes once animation completes, exposes:
      • self animated element

Applies selected CSS object styles in animation. Unlike jQuery it only applies temporary transition property to the animated elements.

$("button").click(({ $util }) => {
  $("h1").animate(
    {
      marginLeft: 200,
      backgroundColor: "red",
    },
    {
      length: "1s", // or 'length: 1000'
      easing: $util.bez("easeInOutCubic"), // cubic-bezoar(0.65, 0, 0.35, 1)
      callback: ({ self }) => {
        // In callback we can infinitely execute more animations if needed
        $(self).animate({
          marginLeft: "0px",
          backgroundColor: "transparent",
        })
      },
    }
  )
})

Usage #2 parameters:

  • callback which exposes:
    • start starts the animation, can be called multiple times. Takes in the same parameters as usage #1
      • properties
      • options
    • self animated element

A different and more flexible approach to animation. Allows you to essentially execute keyframed animation in any length you need. You can decide when to trigger each keyframe and so on. You also gain space to execute any kind of code you want before you start the animation.

$("button").click(({ $util }) => {
  $("h1").animate(async ({ self, $util, start }) => {
    // Execute code before animation begins

    // For example, I can get the element's width and use that
    // const marginLeft = $util.getStyleProperty(self, "width")

    // Or make an API call and then execute animation when it resolves
    await start(
      {
        marginLeft: "200px",
        backgroundColor: "red",
      },
      {
        length: 1000,
        easing: $util.bez("easeInOutCubic"),
      }
    )

    await start({
      marginLeft: 0,
      backgroundColor: "transparent",
    }).then(() => {
      // You can attach .then() to the last keyframe to detect when animation completes
    })
  })
})

Fade in

Parameters:

  • to default: 1, specify opacity to which the element fades in
  • options - $.animate() options object
    • length (optional) default: 500 ms
    • easing (optional) default: ease-in-out
    • callback (optional) executes once animation completes, exposes:
      • self animated element

Applies opacity fade in effect to an element. You can specify at what value it stops.

$("button").click(() => {
  // Content class elements will show up with opacity 0.95
  $(".content").fadeIn(0.95)
})

Fade out

Parameters:

  • to default: 0, specify opacity to which the element fades out
  • options - $.animate() options object
    • length (optional) default: 500 ms
    • easing (optional) default: ease-in-out
    • callback (optional) executes once animation completes, exposes:
      • self animated element

Applies opacity fade out effect to an element. You can specify at what value it stops.

$("button").click(() => {
  // Content class elements will fade-out to opacity 0
  $(".content").fadeOut()
})

Fade Toggle

Parameters:

  • from default: 0
  • to default: 1
  • options - $.animate() options object
    • length (optional) default: 500 ms
    • easing (optional) default: ease-in-out
    • callback (optional) executes once animation completes, exposes:
      • self animated element

Toggles between $.fadeIn and $.fadeOut().

$("button").click(() => {
  $(".content").fadeToggle(0, 0.5, { length: 500, easing: "linear" })
})

Each

Parameters:

  • callback, exposes:
    • self current iterated element
    • prev previous element in iteration
    • index index of iteration

Iterates over selected elements and calls callback function on each iteration

$("ul")
  .children()
  .each(({ self, index }) => {
    $(self).text(`I am ${index + 1}}.`)
  })

Async each

Parameters:

  • callback, exposes:
    • self current iterated element
    • prev previous element in iteration
    • index index of iteration
    • next function that must be called to execute next iteration

Works exactly like $.each() but to execute next iteration, you must call the next() function. This iterator is good for chaining data fetching and other async actions which could cause issues when all called instantly.

$(".users").asyncEach(({ next, self }) => {
  const users = $(self).attr("data-users-endpoint")

  new Promise((resolve) => fetch(users))
    .then((response) => response.json())
    .then((response) => {
      $(self).addChild(response.template)
      next()
    })
})

Filter

Parameters:

  • callback exposes:
    • self iterated element
    • index

Iterates over matched elements and removes those which do not match the provided contition.

const everyEvenChild = $("ul")
  .children()
  .filter(({ index }) => index % 2 === 0)
  .get()

Append and Prepend

Inserts new element(s) either in front or after matched elements

Usage #1 parameters:

  • template template string
$("blockquote").prepend("<label>And so they said:</label>")

Usage #2 parameters:

  • callback exposes:
    • self element which we will add the new element to
    • render render function
$("input").prepend(({ self }) => {
  const labelText = $(self).attr("data-label")

  return `<label>${labelText}</label>`
})

If you are familiar with modern frameworks, you must have heard the term render function. Well jCurry's render functions have exactly the same syntax. For reference Vue documentation

$("input").prepend(({ self }) => {
  const labelText = $(self).attr("data-label")

  return render("label", { title: labelText }, labelText)
})

Add Child

Parameters:

  • callback exposes:
    • self element which we will add the new element to
    • render render function
  • append default: true - wether to insert new elements before or after existing children

Works exactly the same way as $.append and $.prepen except it creates the new element as a child node of the matched elements.

$(".list-wrap").addChild(({ $util, render }) => {
  // Generate an array of 5 items starting at index 1
  const items = $util.from(5, 1).map((item) => {
    return render("li", `#${item} List item`)
  })

  // Create an unordered list and append the items as its children
  return render("ul", { class: ".list" }, items)
})

Text

Parameters:

  • text
  • location default: replace - replaces elements text
    • allowed values: replace, before OR prepend, after OR append

Replaces or appends text to the matched elements.

$("p").text("I am the paragraph now")
$("p").text("The text is as follows:", "before")
$("p").text(".", "after")

Attr

Parameters:

  • property
  • value

Get the value of an attribute for the first element in the set of matched elements or set one or more attributes for every matched element.

// Returns value of a single attribute
const path = $("a").attr("href")
// Returns array of values from selected attributes
const [path, title] = $("a").attr(["href", "title"])

// Assigns value to key
$("a").attr("href", "path/to/page")

// Will throw a warning and skip this chain node
$("a").attr("href", ["path/to/page", "other/to/pat"])

// Assigns key:value pair based on index
$("a").attr(["href", "title", "data-iterations"], ["/hello", "Hello World", 10])

// Sets the same value for every key
$("a").attr(["title", "data-description"], "I am the same text!!!")

Del

Removes matched elements from the DOM.

$("ul")
  .children()
  .filter(({ self, index }) => index % 2 === 0)
  .del()

curry.js's People

Contributors

dolanske avatar

Stargazers

Andrew Lake avatar

Watchers

 avatar

curry.js's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.