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

lgc

v1.0.0

Published

An awesome logic library for JS

Downloads

2

Readme

Logic

GitHub npm build David

What is Logic?

Logic is a library for handling logical operations with functional programming.

Installation

To use with node:

npm install lgc

To access the library:

const lgc = require('lgc')

Usage

Logic

In the heart of the library, there lies Logic function. Not to be confused, it is not a constructor. However it is the function that we use to create logical fragments. You can import Logic directly from library:

const { Logic } = require('lgc')

Logic is a function that utilises functional chaining:

const amIHappy = Logic(isTodayHoliday()).or(isWeatherSunny()).value

Logic also supports autoboxing Logic objects. Hence you have to use value property when you want to get the boolean value since each operation returns another Logic Object instead of Boolean value:

const result = Logic(Logic(true).xor(false)).value

Logic supports 4 basic logical operators: and, or, xor and not

not Operator

Not operator is a special case. It is the only unary function. Using it is a little bit different than others:

const { __ } = require('lgc')
const gte = (x, y) => x >= y
const result = Logic(__, gte(3, 5)).value

__ is a special object which contains one key as @@logical/not. When it is used as first parameter, Logic function -instead of returning Logic object- returns the result of not operation. Also you can use not as a normal operator. However keep in mind that using not explicitly will remove early value which comes from the function chain. Because of that I highly discourage you from using it explicitly.

// Even though we have true as our previous value, it will be discarded in not operation
const isTrue = Logic(true).not(false).value

A Basic Example

Let us try to convert (x || y) && !(x ^ (y && !x)) to a reusable logical fragment chain:

// L is just a shorthand version of Logic
const { L } = require('lgc')
const myLogicalFragment = (x, y) => L(L(x).or(y)).and(L(__, L(L(x).xor(L(y).and(L(__, x))))))

Quite a mess, isn't it? No worry! There is another way of computing logical operations using Logic library. However by chaining we can access and use previous values. This is a big plus which makes up for the enormous complexity.

Pure Operators

Logic library is not only restricted with Logic function. There is a purer(!) way of using logical operators:

const { and, or, not, xor } = require('lgc')
console.log(and(true)(false))
console.log(or(false, false))
console.log(not(false))
console.log(xor(true)(true))

Using those basic functions you can easily compute the result of a logical operation. They are also curried which means they can be either used as op(x, y) or op(x)(y) which let's you those operators with a constant parameter:

const alwaysFalse = and(false)
console.log(alwaysFalse(true))

They can't be chained contrary to Logic object.

Utility Functions

ternary(predicate: (any) => Boolean, trueFunction: Function, falseFunction: Function, deps: Array): Functional implementation of ternary if operator.
Bool(v: any): Takes a value and converts it to the boolean. Applicable for Logic objects.
T: true: A variable for true
F: false: A variable for false

License

The project is licensed under MIT Open-source License.