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

typeself

v0.9.7

Published

JavaScript only has basic [Data types](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Data_structures_and_types) and [typeof ](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof)operator ret

Downloads

10

Readme

typeself provides self-referential types to JavaScript/TypeScript

JavaScript only has basic Data types and typeof operator returns a string indicating the type of the unevaluated operand.

TypeScript brings you optional static type-checking, however, it does not provide a type that has a totally unique property.

typeself ( https://github.com/kenokabe/typeself )

provides a unique self-referential type with the name of the function.

As the most simple example, a unique type: Type(Member) is defined by an identity function: Member = a => a

const  Member  = (a) =>  Type(Member)(a);

Here, the output of the function: Member is a Proxy of a.

Now, define alice that has a string value as usual and bob also has a string value, but in this case, has given type Member.

const  alice  =  "Alice";
const  bob  =  Member("Bob");

The typed data is, in fact, a proxy of the original and behaves like the original. The proxy objects are rarely distinguishable than the original object. The proxy object is noticeable on, for instance, console.log.

isType(Member) returns Boolean(true/flase) for the data with Type(Member) or non-typed.

log(
"Member status of "  +  alice
);//Member status of Alice
log(
isType(Member)(alice)
);//false
log(
"Member status of "  +  bob
);//Member status of Bob
log(
isType(Member)(bob)
);//true

The original concept introduced in SICP as "TypeTag".

ESM

ES Modules are distributed as

  • ./dist/build/modules/typeself.js
  • ./dist/build/modules/primitive-obj.js
import { Type, isType } from "./dist/build/modules/typeself.js";

CJS

npm

Using npm:

$ npm i typeself

In Node.js:

const { Type, isType } = require("typeself");

Test

$ node -r esm ./dist/build/index.js

./test/test-typeself.js

import { log } from "./log";
import { Type, isType } from "../modules/typeself";

const test_typeself = () => {
  log("=Are you a member??? ========= ");
  const Member = (a: string) => Type(Member)([a]);
  const alice = "Alice";
  const bob = Member("Bob");
  log(
    "Member status of " + alice
  );//Member status of Alice
  log(
    isType(Member)(alice)
  );//false
  log(
    "Member status of " + bob
  );//Member status of Bob
  log(
    isType(Member)(bob)
  );//true

  log("=Is this a special operation??========= ");
  const specialOperation = (f: Function) => Type(specialOperation)(f);

  const f1 = (a: number) => a + 1; //vanilla function
  const f2 = Type(specialOperation) //typed function
    ((a: number) => {
      //This function might be considered to be "special" 
      //because it does some featured operations in a context.
      return a * 2;
    });

  log(
    isType(specialOperation)(f1)
  );//false
  log(
    f1(1) // f1 = a => a +1
  );//2  // just in case, let you know
  log(
    isType(specialOperation)(f2)
  );//true
  log(
    f2(1) // f2 = a => a * 2
  );//2  // just in case, let you know

  log("=type test of nontyped=========================");
  const I = (a: undefined) => a;  //just a dummy function

  log(
    isType(I)(I) // true
  );
  log(
    isType(I)(1) // false
  );
  log(
    isType(I)([]) // false
  );
  log(
    isType(I)({}) // false
  );
  log(
    isType(I)("hello") //fakse
  );
  log(
    isType(I)((x: undefined) => x) // false
  );
  log(
    isType(I)(true) // false
  );
  log(
    isType(I)(false) // false
  );

  log("=type test of typed=========================");

  log(
    isType(I)(Type(I)(I)) // true
  );
  log(
    isType(I)(Type(I)(1)) // true
  );
  log(
    isType(I)(Type(I)([])) // true
  );
  log(
    isType(I)(Type(I)({})) // true
  );
  log(
    isType(I)(Type(I)("hello")) //true
  );
  log(
    isType(I)(Type(I)((x: undefined) => x)) // true
  );
  log(
    isType(I)(Type(I)(true)) // true
  );
  log(
    isType(I)(Type(I)(false)) // true
  );
  log(
    (Type(I)(false) == false)
      ? "Type(I)(false) == false  (as should be)"
      : "something is wrong"
  );
  log(
    (Type(I)(false) !== false)//Object !== Primitive
      ? "Type(I)(false) !== false  (as should be)"
      : "something is wrong"
  );
  log(
    isType(I)(Type(I)(NaN)) //true
  );
  log(
    isType(I)(Type(I)(undefined)) // false
  );
  log(
    isType(I)(Type(I)(null)) // false
  );

  log("check---------------------------");
  log(
    Type(I)(1) + Type(I)(2)//3
  );
  log(
    Type(I)([1, 2, 3])//[1, 2, 3]
  );

  log("check---------------------------");
  const n = Type(I)(6);
  log(
    n.toString()
  );


};

export { test_typeself };