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

@dark-elixir/ex-module

v0.2.1

Published

Forbidden dark Elixir of an alchemist

Downloads

3

Readme

ExModule

Forbidden dark Elixir of an alchemist

Motivation

Replicate the module system of Elixir to achieve polymorphism in a serializable state.

Usage

defmodule

# In Elixir
defmodule MyApp.Modules.ExampleModule do
  def greet(name) when is_bitstring(name) do
    "Hi #{name}."
  end
end
// modules/example-module.ts
import {ExModuleDef} from 'ex-module';

// defmodule ------
export namespace ExampleModule {
  export const __exModule__ = 'MyApp.Modules.ExampleModule';

  export function greet(name: string): string {
    return `Hi ${name}.`;
  }
}
ExModuleDef.verify(ExampleModule);

// use module ------
console.log(ExampleModule.greet('Me'));

defstruct

# In Elixir
defmodule MyApp.Modules.ExampleStruct do
  defstruct [:name]

  def greet(%__MODULE__{name}) do
    "Hi #{name}."
  end
end
// modules/example-struct.ts
import {ExStructDef} from 'ex-module';

// defmodule, defstruct ------
export namespace ExampleStruct {
  export const __exModule__ = 'MyApp.Modules.ExampleStruct';
  export const __meta__ = ExStructDef.meta<T>(ExampleStruct);

  export type T = ExStructDef.DefExStruct<
    typeof __exModule__,
    {
      name: string;
    }
  >;

  export function create(name: string): T {
    return __meta__.gen({name});
  }

  export function greet({name}: T): string {
    return `Hi ${name}.`;
  }
}
ExStructDef.verify<ExampleStruct.T>(ExampleStruct);

// use module ------
const me = ExampleStruct.create('Me');
console.log(ExampleStruct.greet(me));

defprotocol, defimpl

# in Elixir
defprotocol Say do
  @spec type(t) :: String.t()
  def say(v)
end

defmodule Gentleman do
  defstruct [:greet]

  defimpl Say, for: Gentleman do
    def greet(%Gentleman{name}, target), do
      IO.inspect("#{greed}, Sir.")
    end
  end
end

defmodule SwampMan do
  defstruct [:name]

  defimpl Say, for: SwampMan do
    def greet(%SwampMan{name}, target) do
      IO.inspect("Hello #{target}. Im #{name}.")
    end
  end
end
// protocols/say.ts
import {ExProtocol, ExStruct} from 'ex-module';
import {ImplSayForSwampMan, SwampMan} from '../modules/swamp-man';
import {Gentleman, ImplSayForGentleman} from '../modules/gentleman';

export interface SayProtocol<Base extends ExStruct> {
  /**
   * Play greeting and update self.
   *
   * @param v Self.
   * @param target Greeting target.
   */
  greet<S extends Base>(v: S, target: string): S;
}

export namespace Say {
  export type T = SwampMan.T | Gentleman.T;

  const genMethod = ExProtocol.accumulate<SayProtocol<T>>({
    [SwampMan.__exModule__]: new ImplSayForSwampMan(),
    [Gentleman.__exModule__]: new ImplSayForGentleman(),
  });

  /**
   * Play greeting and update self.
   *
   * @param v Self.
   * @param target Greeting target.
   */
  export const greet = genMethod('greet');
}

// modules/gentleman.ts
import {ExStructDef} from 'ex-module';
import {SayProtocol} from '../protocols/say';

export namespace Gentleman {
  export const __exModule__ = 'MyApp.Modules.Gentleman';
  export const __meta__ = ExStructDef.meta<T>(Gentleman);

  export type T = ExStructDef.DefExStruct<
    typeof __exModule__,
    {
      greed: string;
    }
  >;

  export function create(greed: string): T {
    return __meta__.gen({greed});
  }
}
ExStructDef.verify<Gentleman.T>(Gentleman);

// defimpl ------
type T = Gentleman.T;
export class ImplSayForGentleman implements SayProtocol<T> {
  greet<S extends T>(v: S, target: string): S {
    console.log(`${v.greed}, Sir ${target}.`);
    return v;
  }
}

// modules/swamp-man.ts
import {ExStructDef} from 'ex-module';
import {SayProtocol} from '../protocols/say';

export namespace SwampMan {
  export const __exModule__ = 'MyApp.Modules.SwampMan';
  export const __meta__ = ExStructDef.meta<T>(SwampMan);

  export type T = ExStructDef.DefExStruct<
    typeof __exModule__,
    {
      name: string;
    }
  >;

  export function create(name: string): T {
    return __meta__.gen({name});
  }
}
ExStructDef.verify<SwampMan.T>(SwampMan);

// defimpl ------
type T = SwampMan.T;
export class ImplSayForSwampMan implements SayProtocol<T> {
  greet<S extends T>(v: S, target: string): S {
    console.log(`Hello ${target}. Im ${v.name}.`);
    return {...v, name: target};
  }
}


// usage.ts
import {Gentleman} from './modules/gentleman';
import {SwampMan} from './modules/swamp-man';
import {Say} from './protocols/say';

const gentleman = Gentleman.create('Hello');
const newGentleman = Say.greet(gentleman, 'unknown human');
console.log(newGentleman);

const swampMan = SwampMan.create('mud');
const newSwampMan = Say.greet(swampMan, 'gentleman');
console.log(newSwampMan);

const anyMan: Say.T = swampMan as Say.T;
const newAnyMan = Say.greet(anyMan, 'who');
console.log(newAnyMan);