https://github.com/olsonpm/tedent
https://github.com/olsonpm/tedent
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/olsonpm/tedent
- Owner: olsonpm
- License: other
- Created: 2018-07-26T22:44:51.000Z (almost 8 years ago)
- Default Branch: dev
- Last Pushed: 2021-03-23T16:43:27.000Z (about 5 years ago)
- Last Synced: 2025-01-20T23:45:41.335Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 8.79 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license.txt
Awesome Lists containing this project
README
# Tedent
Keep your multi-line templated strings lookin' good :sunglasses:
**Table of Contents**
- [What is it?](#what-is-it)
- [What does the name stand for?](#what-does-the-name-stand-for)
- [Why create it?](#why-create-it)
- [Simple Usage](#simple-usage)
- [How the indentation works](#how-the-indentation-works)
- [Important Usage Notes](#important-usage-notes)
- [edge-cases and input requirements](#edge-cases-and-input-requirements)
- [Test](#test)
### What is it?
- A function similar to [dedent](https://github.com/dmnd/dedent) just with
different semantics
### What does the name stand for?
- `Te`mplate string
- in`dent`ation
names are hard
### Why create it?
- dedent didn't handle the following case like I wanted
```js
//
// any multi-line indented string will do, but stringifying an object is the
// common case for me
//
const boroughs = ['Brooklyn', 'Manhattan'],
boroughsString = JSON.stringify(boroughs, null, 2)
console.log(
dedent(`
New York boroughs
${boroughs}
`)
)
/*
expected:
New York boroughs
[
"Brooklyn",
"Manhattan"
]
actual:
New York boroughs
[
"Brooklyn",
"Manhattan"
]
*/
```
### Simple Usage
```js
import tedent from 'tedent'
console.log(
tedent(`
This will be indented
as you expect
`)
)
// writes:
// This will be indented
// as you expect
```
### How the indentation works
The indentation logic is fairly convoluted in order to make the following work
```js
const jstring = anObject => JSON.stringify(anObject, null, 2)
console.log(
tedent(`
header
object 1: ${jstring(object1)}
object 2: ${jstring(object2)}
`)
)
//---------
// outputs
//---------
// header
//
// object 1: {
// ...properly indented object1 contents...
// }
//
// object 2: {
// ...properly indented object2 contents...
// }
//
```
Because the indentation logic is both young and convoluted, please refer to
[the code](index.js) and [tests](test.js) for details. The library is not that
big and if you have any questions please create a github issue.
### Important Usage Notes
- First of all, this library doesn't handle tabs. I will accept a PR
with support
- Secondly, if you always use `tedent` like the following
```js
tedent(`
at least one line
`)
```
then you shouldn't run into any issues. However we all know input can be
tricky so `tedent` has a few edge-cases built-in as well as input requirements
#### edge-cases and input requirements
- if the first argument is anything but `undefined` or `typeof 'string'` then an error will be thrown
- if you pass `undefined` an empty string is returned
- if you pass a string with three or more lines, then
- the first and last lines must contain only whitespace
- the second line must contain a non-whitespace character
- _an error will be thrown if the above two conditions are not met_
- if you pass a string with fewer than 3 lines
- if they only contain whitespace then an empty string is returned
- otherwise an error is thrown
- finally, all trailing whitespace from the result is trimmed
I didn't feel it necessary to explain the reasons for my choices in handling
edge-cases, but if you have questions please ask via github issues.
### Test
`./run test`