# codelike

> Utility for creating codelike objects in javascript

Latest version **0.1.7** (published 2017-08-24) · SEE LICENCE IN LICENSE license · 0 weekly downloads

## Install

```sh
npm install codelike
pnpm add codelike
yarn add codelike
bun add codelike
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.7 |
| Published | 2017-08-24 |
| First published | 2017-08-15 |
| Weekly downloads | 0 |
| License | SEE LICENCE IN LICENSE |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Jonathan Essex |
| Maintainers | softwareplumber |
| Keywords | operator, overloading, actions, fluent |

## Links

- npm: https://www.npmjs.com/package/codelike
- Repository: https://github.com/softwareplumbers/codelike
- Homepage: https://github.com/softwareplumbers/codelike#readme
- Issues: https://github.com/softwareplumbers/codelike/issues
- npm.io page: https://npm.io/package/codelike

## Dependencies (1)

- [debug](https://npm.io/package/debug.md) ^3.0.0

## Recent versions

- 0.1.7 (latest) — 2017-08-24
- 0.1.6 — 2017-08-24
- 0.1.5 — 2017-08-24
- 0.1.4 — 2017-08-16
- 0.1.3 — 2017-08-16
- 0.1.2 — 2017-08-15
- 0.1.1 — 2017-08-15
- 0.1.0 — 2017-08-15

## README

# codelike

Codelike is a utility for creating code-like obects in javascript. For example:

```javascript
import Codelike from codelike;

const codelike = new Codelike('Action', []);
const site = codelike.builder;

const action = site.authenticate('jonathan', 'password1').then(site.popup.close().when(site.authenticated);
```

This looks like ordinary code but the resulting `action` object does not actually do anything. The structure of the statement is simply stored away in a format we can use for our own purposes. The 'codelike' object can be used to convert the structure into a string, so:

```javascript
codelike.stringify(action)
```

will return a string that looks very like the original statement used to create action. So far, so useless. To really use codelike we need to create a formatter object.

```javascript
class myOwnStringify {

    constructor()   { this.out=""; }
    visitRoot(name) { this.out += name; }
    result()        { return this.out; }

    visitThen(first, then) { 
        first.visit(this); this.out+=';' then.visit(this); 
    }
    
    visitWhen(action, condition) {
        this.out+="if("; condition.visit(this); this.out+=")"
        this.out+="then"; action.visit(this);
    }
    
    visitGet(first, name) {
        first.visit(this);
        this.out+=`\${name}`;
    }

    visitCall(accessor, name, parameters) {
        accessor.visit(this); this.out+=':';
        this.out+=`\${name}(${parameters.join(','))`
    }
}
```

This formatter object will output the code in an entirely different syntax. To use your own formatters, simply do:

```javascript
const codelike = new Codelike('MyAction', [ myOwnStringify ]);
```

then codelike.myOwnStringify(action) will return a formatted string (or any other type of object) to your own requirements as formatted by myOwnStringify.

---
_Source: https://npm.io/package/codelike · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
