Code Monkey home page Code Monkey logo

endent's Introduction

Endent stability

npm travis dm js-standard-style

An ES6 string tag that makes indentation right, adds some key features to dedent.

Migrate

Because I lost my 2 factor authentic token, this project has moved to https://github.com/zhouhanseng/endent

Feature

Pretty object

import dedent from "dedent";
import endent from "endent";

var someobj = {
  contact: {
    jack: 123456,
    tom: 654321,
  },
  color: "blue",
};

var somejson = '["bear", "fish", "dog", "cat"]';

var awfulTmpl = dedent`
  function store (state, emitter) {
    state["someobj"] = ${JSON.stringify(someobj, null, 2)}
    state["somejson"] = ${JSON.stringify(JSON.parse(somejson), null, 2)}
  }
`;
// use endent.pretty(value) when value is object or array.
var prettyTmpl = endent`
  function store (state, emitter) {
    state["someobj"] = ${endent.pretty(someobj)}
    state["somejson"] = ${somejson}
  }
`;

console.log(awfulTmpl + "\n\n" + prettyTmpl);
// awfulTmpl
function store(state, emitter) {
  state["someobj"] = {
    contact: {
      jack: 123456,
      tom: 654321,
    },
    color: "blue",
  };
  state["somejson"] = ["bear", "fish", "dog", "cat"];
}

// prettyTmpl
function store(state, emitter) {
  state["someobj"] = {
    contact: {
      jack: 123456,
      tom: 654321,
    },
    color: "blue",
  };
  state["somejson"] = ["bear", "fish", "dog", "cat"];
}

Endows suitable indentation for multiline interpolation

var dependencies = ["jquery", "underscore", "bootstrap"];
var dependencyTmpl = ``;
dependencies.forEach((d, i) => {
  dependencyTmpl += `var ${d} = require("${d}")\n`;
});

var awfulTmpl = dedent`
  ;(function () {
    ${dependencyTmpl}
  })()
`;

var prettyTmpl = endent`
  ;(function () {
    ${dependencyTmpl}
  })()
`;

console.log(awfulTmpl + "\n\n" + prettyTmpl);
// awfulTmpl
(function () {
  var jquery = require("jquery");
  var underscore = require("underscore");
  var bootstrap = require("bootstrap");
})();

// prettyTmpl
(function () {
  var jquery = require("jquery");
  var underscore = require("underscore");
  var bootstrap = require("bootstrap");
})();

License

MIT

endent's People

Contributors

dependabot[bot] avatar dpogue avatar dword-design avatar zhouhanseng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

endent's Issues

Trim only one line at start and end

Currently endent trims all start and end newlines. But it would be more flexible to only trim max 1 newline. This way we can create newlines in the template string. Problem is that the logic is in dedent and my PR is not getting merged. Also, I don't know what the state of the project is. Maybe some feedback here how to proceed. Here is the link to my PR in dedent:

dmnd/dedent#30

Invalid "main" field in objectorarray package.json breaks endent

endent depends on objectorarray which has an error in its package.json, it declares "main": "dist/index.js" but the package published on npm does not contain a /dist folder:

image

This is causing all sorts of issues with endent.
For example, if you try to compile an app containing endent with Rollup or Vite, the build command will fail with this error:

[commonjs] Failed to resolve entry for package "objectorarray". The package may have incorrect main/module/exports specified in its package.json

Node.js 16 is also showing a warning with a similar content:

DeprecationWarning: Invalid 'main' field in '\node_modules\objectorarray\package.json' of 'dist/index.js'

I'm opening an issue here because I believe that @zhouhanseng is one of the authors of both endent and objectorarray.
There are two open issues (issue 3 and issue 7) about this in the objectorarray repo.

The compiled output is using import syntax

The compiled Javascript output must use require and not import, since the import is not yet supported in Node.js properly.

This is happening, coz you are using esnext for the module property inside the tsconfig.json file.

Mind fixing it and push a new release?

Multiline interpolation breaks with non-whitespace prefix

Given that

endent`
    foo.
    ${"hello\nworld"}
    bar.
`

correctly comes out as

foo.
hello
world
bar.    

I expect this:

endent`
    foo.
    x=${"hello\nworld"}
    bar.
`

to come out as

foo.
x=hello
  bar
bar.

but instead it comes out as

foo.
x=hello
world
bar.

and when I try to add the spaces on the second line myself

endent`
    foo.
    x=${"hello\n  world"}
    bar.
`

then it comes out even worse

foo.
    x=hello
world
    bar.

JSON formatting is broken

If I do this:

console.log(endent`
      hello

      \`\`\`json
      ${endent.pretty({ a: 1, b: 2, c: { foo: 3, bar: 4 } })}
      \`\`\`
      
      world
`)

Or this:

const myJson = JSON.stringify({ a: 1, b: 2, c: { foo: 3, bar: 4 } })
console.log(endent`
      hello

      \`\`\`json
      ${myJson}
      \`\`\`
      
      world
`)

I get this poorly-formatted output:

hello

    ```json
    {
"a": 1,
"b": 2,
"c": {
  "foo": 3,
  "bar": 4
}
}
    ```

    world

But I expect this:

hello

```json
{
  "a": 1,
  "b": 2,
  "c": {
    "foo": 3,
    "bar": 4
  }
}
```

world

However, if I put some text before my object:

console.log(endent`
      hello

      \`\`\`json
      REMOVE_ME${endent.pretty({ a: 1, b: 2, c: { foo: 3, bar: 4 } })}
      \`\`\`
      
      world
`)

It formats everything correctly (but then I need to remove the REMOVE_ME bit in a separate step):

hello

```json
REMOVE_ME{
  "a": 1,
  "b": 2,
  "c": {
    "foo": 3,
    "bar": 4
  }
}
```

world

I was going to file this over at https://github.com/zhouhanseng/endent but you have issues disabled there.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.