simple way to turn imperative code declarative

Usage no npm install needed!

<script type="module">
  import declarativeEngine from '';


Declarative Engine

CI npm package

simple way to turn imperative code declarative

It is no secret that I am a huge fan of GraphQL. But I tend to get lots of push back when suggesting it as a solution to a problem. The pushback is always related to not wanting to learn a new query language and type system. Another argument is for not wanting to bring in huge libraries.

This project is an attempt at putting those arguments to rest by extracting the declarative -> imperative pattern that we all love so much and keeping it dead simple.


{yarn, npm} add declarative-engine


Create a declarative engine like this:

import createEngine from "declarative-engine";

const executeDeclarativeEngine = createEngine({
  Ball: {
    volume(obj) {
      return (4 / 3) * Math.PI * obj.radius ** 3;
  Basket: {
    height(obj) {
      return `${obj.height} ft`;
  typeFromObj: obj => obj.type

Finally, you can use it like this:

const result = executeDeclarativeEngine({
  type: "Court",
  ball: {
    type: "Ball",
    radius: 10,
    volume: true
  basket: {
    type: "Basket",
    height: 12


And it should Print:

      "height":"12 ft"