Initial commit
This commit is contained in:
+1
@@ -0,0 +1 @@
|
||||
../prettier/bin-prettier.js
|
||||
+1
@@ -0,0 +1 @@
|
||||
../ulid/bin/cli.js
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2021 Niklas Portmann
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
+197
@@ -0,0 +1,197 @@
|
||||
# prettier-plugin-go-template
|
||||
|
||||
[](https://www.npmjs.com/package/prettier-plugin-go-template) [](https://codecov.io/gh/NiklasPor/prettier-plugin-go-template) [](#contributors-)
|
||||
|
||||
Formatter plugin for go template files. The only peer dependency is [prettier](https://www.npmjs.com/package/prettier). Test you own code on the [**playground**](https://prettier-plugin-go-template-playground.niklaspor.dev/).
|
||||
|
||||
```bash
|
||||
npm install --save-dev prettier prettier-plugin-go-template
|
||||
```
|
||||
|
||||
The following file types are detected automatically:
|
||||
`.gohtml`, `.gotmpl`, `.go.tmpl`, `.tmpl`, `.tpl`, `.html.tmpl`
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<th>Input</th>
|
||||
<th>Output</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
|
||||
<!-- prettier-ignore-start -->
|
||||
```html
|
||||
{{ if or .Prev .Next -}}
|
||||
{{ $p := where site.Pages }}
|
||||
<div class="my-navigation">
|
||||
{{ with $p.Next . -}}
|
||||
<a href="{{ .RelPermalink }}">
|
||||
<div class="row">
|
||||
<div class="cell py-2">
|
||||
{{ .Title }}
|
||||
</div> </div> </a>
|
||||
{{ end -}}
|
||||
</div>
|
||||
{{ end -}}
|
||||
```
|
||||
<!-- prettier-ignore-end -->
|
||||
|
||||
</td>
|
||||
<td>
|
||||
|
||||
<!-- prettier-ignore-start -->
|
||||
```html
|
||||
{{ if or .Prev .Next -}}
|
||||
{{ $p := where site.Pages }}
|
||||
<div class="my-navigation">
|
||||
{{ with $p.Next . -}}
|
||||
<a href="{{ .RelPermalink }}">
|
||||
<div class="row">
|
||||
<div class="cell py-2">{{ .Title }}</div>
|
||||
</div>
|
||||
</a>
|
||||
{{ end -}}
|
||||
</div>
|
||||
{{ end -}}
|
||||
```
|
||||
<!-- prettier-ignore-end -->
|
||||
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
## GoHugo / `.html`
|
||||
|
||||
To use it with GoHugo and basic `.html` files, you'll have to override the used parser inside your `.prettierrc` file:
|
||||
|
||||
```js
|
||||
{
|
||||
overrides: [
|
||||
{
|
||||
files: ["*.html"],
|
||||
options: {
|
||||
parser: "go-template",
|
||||
},
|
||||
},
|
||||
],
|
||||
}
|
||||
```
|
||||
|
||||
## VSCode
|
||||
|
||||
Make sure to always have installed **both** dependencies:
|
||||
|
||||
- `prettier`
|
||||
- `prettier-plugin-go-template`
|
||||
|
||||
Also make sure that they are installed inside the same scope.
|
||||
Install both globally (`npm i -g`) or locally – otherwise prettier may not pick up the plugin.
|
||||
|
||||
> Note: The global setup additional requires setting your VSCode prettier path to your global prettier path. You can read in [this issue](https://github.com/NiklasPor/prettier-plugin-go-template/issues/58#issuecomment-1085060511) how to set it up – should be doable in less than a minute if you have npm & VSCode already running.
|
||||
|
||||
## Additional Options
|
||||
|
||||
```js
|
||||
// .prettierrc
|
||||
{
|
||||
/**
|
||||
* Enables & disables spacing between go statements.
|
||||
* E.g. {{ statement }} vs {{statement}}.
|
||||
* Default: true
|
||||
*/
|
||||
"goTemplateBracketSpacing": true
|
||||
}
|
||||
```
|
||||
|
||||
## Ignoring Code
|
||||
|
||||
#### Single Block
|
||||
|
||||
```html
|
||||
<div>
|
||||
<!-- prettier-ignore -->
|
||||
{{if }}
|
||||
{{end }}
|
||||
</div>
|
||||
```
|
||||
|
||||
#### Multiline
|
||||
|
||||
```html
|
||||
<html>
|
||||
{{/* prettier-ignore-start */}}
|
||||
<script>
|
||||
{{if }}
|
||||
Whatever.
|
||||
{{else }}
|
||||
Psych.
|
||||
{{end }}
|
||||
</script>
|
||||
{{/* prettier-ignore-end */}}
|
||||
</html>
|
||||
```
|
||||
|
||||
## Changelog
|
||||
|
||||
### v0.0.12
|
||||
|
||||
- Fix several formatting issues
|
||||
- Improve unformattable script & style detection
|
||||
- Huge thanks to @jasikpark for validating & cleaning up the issues 🎉
|
||||
|
||||
### v0.0.11
|
||||
|
||||
- AST rewrite
|
||||
- Fix inline actions
|
||||
- If / Else / Else-If support
|
||||
- Ignore formatting for blocks with `{{/* prettier-ignore */}}
|
||||
- Ignore large code sections with `{{/* prettier-ignore-start */}}...{{/* prettier-ignore-end */}}
|
||||
- Tweak general formatting
|
||||
- Support for multiline actions
|
||||
|
||||
### v0.0.10
|
||||
|
||||
- Resolve bug #19: Fix template comments.
|
||||
|
||||
### v0.0.9
|
||||
|
||||
- Resolve bug of single line if statements.
|
||||
|
||||
### v0.0.8
|
||||
|
||||
- Go block statements will now be indented accordingly. Except for `else`.
|
||||
- if, range, block, with, define, end
|
||||
|
||||
### v0.0.7
|
||||
|
||||
- Fix broken shortcodes. Thanks to @alqu for discovering & fixing the bug.
|
||||
|
||||
## Contributors ✨
|
||||
|
||||
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
|
||||
|
||||
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
|
||||
<!-- prettier-ignore-start -->
|
||||
<!-- markdownlint-disable -->
|
||||
<table>
|
||||
<tr>
|
||||
<td align="center"><a href="https://github.com/alqu"><img src="https://avatars1.githubusercontent.com/u/12250845?v=4?s=100" width="100px;" alt=""/><br /><sub><b>alqu</b></sub></a><br /><a href="https://github.com/NiklasPor/prettier-plugin-go-template/issues?q=author%3Aalqu" title="Bug reports">🐛</a> <a href="https://github.com/NiklasPor/prettier-plugin-go-template/commits?author=alqu" title="Tests">⚠️</a> <a href="https://github.com/NiklasPor/prettier-plugin-go-template/commits?author=alqu" title="Code">💻</a></td>
|
||||
<td align="center"><a href="https://www.gabrielmaldi.com"><img src="https://avatars3.githubusercontent.com/u/3728897?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Gabriel Monteagudo</b></sub></a><br /><a href="https://github.com/NiklasPor/prettier-plugin-go-template/issues?q=author%3Agabrielmaldi" title="Bug reports">🐛</a></td>
|
||||
<td align="center"><a href="https://github.com/bgold0"><img src="https://avatars1.githubusercontent.com/u/4645400?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Bryan</b></sub></a><br /><a href="https://github.com/NiklasPor/prettier-plugin-go-template/issues?q=author%3Abgold0" title="Bug reports">🐛</a></td>
|
||||
<td align="center"><a href="http://richtera.org"><img src="https://avatars2.githubusercontent.com/u/708186?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Andreas Richter</b></sub></a><br /><a href="https://github.com/NiklasPor/prettier-plugin-go-template/issues?q=author%3Arichtera" title="Bug reports">🐛</a></td>
|
||||
<td align="center"><a href="https://noahbrenner.github.io/"><img src="https://avatars3.githubusercontent.com/u/24858379?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Noah Brenner</b></sub></a><br /><a href="https://github.com/NiklasPor/prettier-plugin-go-template/commits?author=noahbrenner" title="Code">💻</a> <a href="https://github.com/NiklasPor/prettier-plugin-go-template/commits?author=noahbrenner" title="Documentation">📖</a></td>
|
||||
<td align="center"><a href="https://silverwind.io"><img src="https://avatars1.githubusercontent.com/u/115237?v=4?s=100" width="100px;" alt=""/><br /><sub><b>silverwind</b></sub></a><br /><a href="#ideas-silverwind" title="Ideas, Planning, & Feedback">🤔</a></td>
|
||||
<td align="center"><a href="https://codeberg.org/cpence"><img src="https://avatars0.githubusercontent.com/u/297075?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Charles Pence</b></sub></a><br /><a href="https://github.com/NiklasPor/prettier-plugin-go-template/issues?q=author%3Acpence" title="Bug reports">🐛</a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="center"><a href="http://jasik.xyz"><img src="https://avatars.githubusercontent.com/u/10626596?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Caleb Jasik</b></sub></a><br /><a href="https://github.com/NiklasPor/prettier-plugin-go-template/issues?q=author%3Ajasikpark" title="Bug reports">🐛</a> <a href="https://github.com/NiklasPor/prettier-plugin-go-template/commits?author=jasikpark" title="Documentation">📖</a> <a href="#example-jasikpark" title="Examples">💡</a> <a href="#ideas-jasikpark" title="Ideas, Planning, & Feedback">🤔</a> <a href="#maintenance-jasikpark" title="Maintenance">🚧</a> <a href="#question-jasikpark" title="Answering Questions">💬</a></td>
|
||||
<td align="center"><a href="http://DanGold.me"><img src="https://avatars.githubusercontent.com/u/8890238?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Dan Gold</b></sub></a><br /><a href="https://github.com/NiklasPor/prettier-plugin-go-template/issues?q=author%3ALandGod" title="Bug reports">🐛</a></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<!-- markdownlint-restore -->
|
||||
<!-- prettier-ignore-end -->
|
||||
|
||||
<!-- ALL-CONTRIBUTORS-LIST:END -->
|
||||
|
||||
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
|
||||
+1
@@ -0,0 +1 @@
|
||||
export declare function createIdGenerator(): () => string;
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.createIdGenerator = void 0;
|
||||
const ulid_1 = require("ulid");
|
||||
function createIdGenerator() {
|
||||
return () => (0, ulid_1.ulid)();
|
||||
}
|
||||
exports.createIdGenerator = createIdGenerator;
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY3JlYXRlLWlkLWdlbmVyYXRvci5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy9jcmVhdGUtaWQtZ2VuZXJhdG9yLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7OztBQUFBLCtCQUE0QjtBQUU1QixTQUFnQixpQkFBaUI7SUFDL0IsT0FBTyxHQUFHLEVBQUUsQ0FBQyxJQUFBLFdBQUksR0FBRSxDQUFDO0FBQ3RCLENBQUM7QUFGRCw4Q0FFQyIsInNvdXJjZXNDb250ZW50IjpbImltcG9ydCB7IHVsaWQgfSBmcm9tIFwidWxpZFwiO1xuXG5leHBvcnQgZnVuY3Rpb24gY3JlYXRlSWRHZW5lcmF0b3IoKTogKCkgPT4gc3RyaW5nIHtcbiAgcmV0dXJuICgpID0+IHVsaWQoKTtcbn1cbiJdfQ==
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
import { Parser, Printer, SupportLanguage } from "prettier";
|
||||
import { GoNode } from "./parse";
|
||||
export declare type PrettierPluginGoTemplateParserOptions = {
|
||||
goTemplateBracketSpacing: boolean;
|
||||
};
|
||||
export declare const options: {
|
||||
[K in keyof PrettierPluginGoTemplateParserOptions]: any;
|
||||
};
|
||||
export declare const languages: SupportLanguage[];
|
||||
export declare const parsers: {
|
||||
"go-template": Parser<GoNode>;
|
||||
};
|
||||
export declare const printers: {
|
||||
"go-template": Printer<GoNode>;
|
||||
};
|
||||
+249
File diff suppressed because one or more lines are too long
+47
@@ -0,0 +1,47 @@
|
||||
import { Parser } from "prettier";
|
||||
export declare const parseGoTemplate: Parser<GoNode>["parse"];
|
||||
export declare type GoNode = GoRoot | GoBlock | GoInline | GoMultiBlock | GoUnformattable;
|
||||
export declare type GoBlockKeyword = "if" | "range" | "block" | "with" | "define" | "else" | "prettier-ignore-start" | "prettier-ignore-end" | "end";
|
||||
export declare type GoRoot = {
|
||||
type: "root";
|
||||
} & Omit<GoBlock, "type" | "keyword" | "parent" | "statement" | "id" | "startDelimiter" | "endDelimiter" | "start" | "end">;
|
||||
export interface GoBaseNode<Type extends string> {
|
||||
id: string;
|
||||
type: Type;
|
||||
index: number;
|
||||
length: number;
|
||||
parent: GoBlock | GoRoot | GoMultiBlock;
|
||||
}
|
||||
export interface GoBlock extends GoBaseNode<"block">, WithDelimiter {
|
||||
keyword: GoBlockKeyword;
|
||||
children: {
|
||||
[id: string]: GoNode;
|
||||
};
|
||||
start: GoInline;
|
||||
end: GoInline | null;
|
||||
content: string;
|
||||
aliasedContent: string;
|
||||
contentStart: number;
|
||||
}
|
||||
export interface GoMultiBlock extends GoBaseNode<"double-block"> {
|
||||
blocks: (GoBlock | GoMultiBlock)[];
|
||||
keyword: GoBlockKeyword;
|
||||
}
|
||||
export declare type GoSharedDelimiter = "%" | "-" | "";
|
||||
export declare type GoInlineStartDelimiter = "<" | "/*" | GoSharedDelimiter;
|
||||
export declare type GoInlineEndDelimiter = ">" | "*/" | GoSharedDelimiter;
|
||||
export interface GoUnformattable extends GoBaseNode<"unformattable"> {
|
||||
content: string;
|
||||
}
|
||||
export interface WithDelimiter {
|
||||
startDelimiter: GoInlineStartDelimiter;
|
||||
endDelimiter: GoInlineEndDelimiter;
|
||||
}
|
||||
export interface GoInline extends GoBaseNode<"inline">, WithDelimiter {
|
||||
statement: string;
|
||||
}
|
||||
export declare function isInline(node: GoNode): node is GoInline;
|
||||
export declare function isBlock(node: GoNode): node is GoBlock;
|
||||
export declare function isMultiBlock(node: GoNode): node is GoMultiBlock;
|
||||
export declare function isRoot(node: GoNode): node is GoRoot;
|
||||
export declare function isUnformattable(node: GoNode): node is GoRoot;
|
||||
+183
File diff suppressed because one or more lines are too long
+113
@@ -0,0 +1,113 @@
|
||||
{
|
||||
"_from": "prettier-plugin-go-template",
|
||||
"_id": "prettier-plugin-go-template@0.0.13",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-gG/xT5kd+kCzoMaTchXvdfBdsunyRCV6G8cgdPGPd2V5JGGKXUG7SjzBKU7jaGh2RTeblcAdBb/E+S/duOAMsA==",
|
||||
"_location": "/prettier-plugin-go-template",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "tag",
|
||||
"registry": true,
|
||||
"raw": "prettier-plugin-go-template",
|
||||
"name": "prettier-plugin-go-template",
|
||||
"escapedName": "prettier-plugin-go-template",
|
||||
"rawSpec": "",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "latest"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"#DEV:/",
|
||||
"#USER"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/prettier-plugin-go-template/-/prettier-plugin-go-template-0.0.13.tgz",
|
||||
"_shasum": "b4047bce76430bc89a8ee8f27fad1b1c14d942be",
|
||||
"_spec": "prettier-plugin-go-template",
|
||||
"_where": "/home/kossa/projects/knaeckeboot-tailwind",
|
||||
"author": {
|
||||
"name": "Niklas Portmann",
|
||||
"email": "niklaspor@gmail.com"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/NiklasPor/prettier-plugin-go-template/issues"
|
||||
},
|
||||
"bundleDependencies": false,
|
||||
"dependencies": {
|
||||
"ulid": "^2.3.0"
|
||||
},
|
||||
"deprecated": false,
|
||||
"description": "Prettier plugin for formatting Go & GoHugo templates.",
|
||||
"devDependencies": {
|
||||
"@rollup/plugin-commonjs": "^17.0.0",
|
||||
"@rollup/plugin-node-resolve": "^11.0.0",
|
||||
"@rollup/plugin-typescript": "^8.0.0",
|
||||
"@tsconfig/svelte": "^2.0.0",
|
||||
"@types/jest": "^26.0.15",
|
||||
"@types/node": "^14.14.2",
|
||||
"@types/prettier": "^2.2.3",
|
||||
"codecov": "^3.8.0",
|
||||
"jest": "^26.6.1",
|
||||
"prettier": "^2.3.0",
|
||||
"rollup": "^2.3.4",
|
||||
"rollup-plugin-css-only": "^3.1.0",
|
||||
"rollup-plugin-livereload": "^2.0.0",
|
||||
"rollup-plugin-svelte": "^7.0.0",
|
||||
"rollup-plugin-terser": "^7.0.0",
|
||||
"sirv-cli": "^2.0.0",
|
||||
"svelte": "^3.0.0",
|
||||
"svelte-check": "^2.0.0",
|
||||
"svelte-preprocess": "^4.0.0",
|
||||
"ts-jest": "^26.4.2",
|
||||
"ts-node": "^9.0.0",
|
||||
"tslib": "^2.0.0",
|
||||
"tslint": "^6.1.3",
|
||||
"tslint-config-prettier": "^1.18.0",
|
||||
"typescript": "^4.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=14.0.0"
|
||||
},
|
||||
"files": [
|
||||
"lib"
|
||||
],
|
||||
"homepage": "https://github.com/NiklasPor/prettier-plugin-go-template#readme",
|
||||
"jest": {
|
||||
"preset": "ts-jest"
|
||||
},
|
||||
"keywords": [
|
||||
"prettier",
|
||||
"plugin",
|
||||
"go",
|
||||
"hugo",
|
||||
"gohugo",
|
||||
"template",
|
||||
"html"
|
||||
],
|
||||
"license": "MIT",
|
||||
"main": "lib/index",
|
||||
"name": "prettier-plugin-go-template",
|
||||
"peerDependencies": {
|
||||
"prettier": "^2.0.0"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/NiklasPor/prettier-plugin-go-template.git"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc --pretty",
|
||||
"check": "svelte-check --tsconfig ./tsconfig.json",
|
||||
"coverage": "jest --coverage --no-cache",
|
||||
"lint": "tslint --project .",
|
||||
"publish:coverage": "codecov -t $npm_config_prettier_plugin_go_html_codecov",
|
||||
"release:coverage": "npm run coverage && npm run publish:coverage",
|
||||
"release:plugin": "npm run build && npm run release:coverage && npm publish",
|
||||
"release:plugin:beta": "npm run build && npm run release:coverage && npm publish --tag beta",
|
||||
"test": "jest",
|
||||
"watch": "npm run build -- --watch",
|
||||
"watch:test": "jest --watch",
|
||||
"website:build": "rollup -c",
|
||||
"website:serve": "rollup -c -w",
|
||||
"website:start": "sirv website/public --no-clear"
|
||||
},
|
||||
"types": "lib/index",
|
||||
"version": "0.0.13"
|
||||
}
|
||||
+6001
File diff suppressed because it is too large
Load Diff
+109
@@ -0,0 +1,109 @@
|
||||

|
||||
|
||||
<h2 align="center">Opinionated Code Formatter</h2>
|
||||
|
||||
<p align="center">
|
||||
<em>
|
||||
JavaScript
|
||||
· TypeScript
|
||||
· Flow
|
||||
· JSX
|
||||
· JSON
|
||||
</em>
|
||||
<br />
|
||||
<em>
|
||||
CSS
|
||||
· SCSS
|
||||
· Less
|
||||
</em>
|
||||
<br />
|
||||
<em>
|
||||
HTML
|
||||
· Vue
|
||||
· Angular
|
||||
</em>
|
||||
<br />
|
||||
<em>
|
||||
GraphQL
|
||||
· Markdown
|
||||
· YAML
|
||||
</em>
|
||||
<br />
|
||||
<em>
|
||||
<a href="https://prettier.io/docs/en/plugins.html">
|
||||
Your favorite language?
|
||||
</a>
|
||||
</em>
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
<a href="https://github.com/prettier/prettier/actions?query=workflow%3AProd+branch%3Amain">
|
||||
<img alt="Github Actions Build Status" src="https://img.shields.io/github/workflow/status/prettier/prettier/Prod?label=Prod&style=flat-square"></a>
|
||||
<a href="https://github.com/prettier/prettier/actions?query=workflow%3ADev+branch%3Amain">
|
||||
<img alt="Github Actions Build Status" src="https://img.shields.io/github/workflow/status/prettier/prettier/Dev?label=Dev&style=flat-square"></a>
|
||||
<a href="https://github.com/prettier/prettier/actions?query=workflow%3ALint+branch%3Amain">
|
||||
<img alt="Github Actions Build Status" src="https://img.shields.io/github/workflow/status/prettier/prettier/Lint?label=Lint&style=flat-square"></a>
|
||||
<a href="https://codecov.io/gh/prettier/prettier">
|
||||
<img alt="Codecov Coverage Status" src="https://img.shields.io/codecov/c/github/prettier/prettier.svg?style=flat-square"></a>
|
||||
<a href="https://twitter.com/acdlite/status/974390255393505280">
|
||||
<img alt="Blazing Fast" src="https://img.shields.io/badge/speed-blazing%20%F0%9F%94%A5-brightgreen.svg?style=flat-square"></a>
|
||||
<br/>
|
||||
<a href="https://www.npmjs.com/package/prettier">
|
||||
<img alt="npm version" src="https://img.shields.io/npm/v/prettier.svg?style=flat-square"></a>
|
||||
<a href="https://www.npmjs.com/package/prettier">
|
||||
<img alt="weekly downloads from npm" src="https://img.shields.io/npm/dw/prettier.svg?style=flat-square"></a>
|
||||
<a href="#badge">
|
||||
<img alt="code style: prettier" src="https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square"></a>
|
||||
<a href="https://twitter.com/PrettierCode">
|
||||
<img alt="Follow Prettier on Twitter" src="https://img.shields.io/twitter/follow/prettiercode.svg?label=follow+prettier&style=flat-square"></a>
|
||||
</p>
|
||||
|
||||
## Intro
|
||||
|
||||
Prettier is an opinionated code formatter. It enforces a consistent style by parsing your code and re-printing it with its own rules that take the maximum line length into account, wrapping code when necessary.
|
||||
|
||||
### Input
|
||||
|
||||
<!-- prettier-ignore -->
|
||||
```js
|
||||
foo(reallyLongArg(), omgSoManyParameters(), IShouldRefactorThis(), isThereSeriouslyAnotherOne());
|
||||
```
|
||||
|
||||
### Output
|
||||
|
||||
```js
|
||||
foo(
|
||||
reallyLongArg(),
|
||||
omgSoManyParameters(),
|
||||
IShouldRefactorThis(),
|
||||
isThereSeriouslyAnotherOne()
|
||||
);
|
||||
```
|
||||
|
||||
Prettier can be run [in your editor](https://prettier.io/docs/en/editors.html) on-save, in a [pre-commit hook](https://prettier.io/docs/en/precommit.html), or in [CI environments](https://prettier.io/docs/en/cli.html#list-different) to ensure your codebase has a consistent style without devs ever having to post a nit-picky comment on a code review ever again!
|
||||
|
||||
---
|
||||
|
||||
**[Documentation](https://prettier.io/docs/en/)**
|
||||
|
||||
<!-- prettier-ignore -->
|
||||
[Install](https://prettier.io/docs/en/install.html) ·
|
||||
[Options](https://prettier.io/docs/en/options.html) ·
|
||||
[CLI](https://prettier.io/docs/en/cli.html) ·
|
||||
[API](https://prettier.io/docs/en/api.html)
|
||||
|
||||
**[Playground](https://prettier.io/playground/)**
|
||||
|
||||
---
|
||||
|
||||
## Badge
|
||||
|
||||
Show the world you're using _Prettier_ → [](https://github.com/prettier/prettier)
|
||||
|
||||
```md
|
||||
[](https://github.com/prettier/prettier)
|
||||
```
|
||||
|
||||
## Contributing
|
||||
|
||||
See [CONTRIBUTING.md](CONTRIBUTING.md).
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
#!/usr/bin/env node
|
||||
"use strict";
|
||||
var __getOwnPropNames = Object.getOwnPropertyNames;
|
||||
var __commonJS = function(cb, mod) {
|
||||
return function __require() {
|
||||
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
||||
};
|
||||
};
|
||||
|
||||
// node_modules/semver-compare/index.js
|
||||
var require_semver_compare = __commonJS({
|
||||
"node_modules/semver-compare/index.js": function(exports2, module2) {
|
||||
module2.exports = function cmp(a, b) {
|
||||
var pa = a.split(".");
|
||||
var pb = b.split(".");
|
||||
for (var i = 0; i < 3; i++) {
|
||||
var na = Number(pa[i]);
|
||||
var nb = Number(pb[i]);
|
||||
if (na > nb)
|
||||
return 1;
|
||||
if (nb > na)
|
||||
return -1;
|
||||
if (!isNaN(na) && isNaN(nb))
|
||||
return 1;
|
||||
if (isNaN(na) && !isNaN(nb))
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
// node_modules/please-upgrade-node/index.js
|
||||
var require_please_upgrade_node = __commonJS({
|
||||
"node_modules/please-upgrade-node/index.js": function(exports2, module2) {
|
||||
var semverCompare = require_semver_compare();
|
||||
module2.exports = function pleaseUpgradeNode2(pkg, opts) {
|
||||
var opts = opts || {};
|
||||
var requiredVersion = pkg.engines.node.replace(">=", "");
|
||||
var currentVersion = process.version.replace("v", "");
|
||||
if (semverCompare(currentVersion, requiredVersion) === -1) {
|
||||
if (opts.message) {
|
||||
console.error(opts.message(requiredVersion));
|
||||
} else {
|
||||
console.error(pkg.name + " requires at least version " + requiredVersion + " of Node, please upgrade");
|
||||
}
|
||||
if (opts.hasOwnProperty("exitCode")) {
|
||||
process.exit(opts.exitCode);
|
||||
} else {
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
// bin/prettier.js
|
||||
var pleaseUpgradeNode = require_please_upgrade_node();
|
||||
var packageJson = require("./package.json");
|
||||
pleaseUpgradeNode(packageJson);
|
||||
var cli = require("./cli.js");
|
||||
module.exports = cli.run(process.argv.slice(2));
|
||||
+15085
File diff suppressed because it is too large
Load Diff
+1392
File diff suppressed because one or more lines are too long
+2
File diff suppressed because one or more lines are too long
+27
File diff suppressed because one or more lines are too long
+26
File diff suppressed because one or more lines are too long
+35
File diff suppressed because one or more lines are too long
+27
File diff suppressed because one or more lines are too long
+15
File diff suppressed because one or more lines are too long
+36
File diff suppressed because one or more lines are too long
+76
File diff suppressed because one or more lines are too long
+19
File diff suppressed because one or more lines are too long
+76
File diff suppressed because one or more lines are too long
+280
File diff suppressed because one or more lines are too long
+150
File diff suppressed because one or more lines are too long
+116
File diff suppressed because one or more lines are too long
+37282
File diff suppressed because one or more lines are too long
+57
@@ -0,0 +1,57 @@
|
||||
{
|
||||
"_from": "prettier",
|
||||
"_id": "prettier@2.7.1",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-ujppO+MkdPqoVINuDFDRLClm7D78qbDt0/NR+wp5FqEZOoTNAjPHWj17QRhu7geIHJfcNhRk1XVQmF8Bp3ye+g==",
|
||||
"_location": "/prettier",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "tag",
|
||||
"registry": true,
|
||||
"raw": "prettier",
|
||||
"name": "prettier",
|
||||
"escapedName": "prettier",
|
||||
"rawSpec": "",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "latest"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"#DEV:/",
|
||||
"#USER"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/prettier/-/prettier-2.7.1.tgz",
|
||||
"_shasum": "e235806850d057f97bb08368a4f7d899f7760c64",
|
||||
"_spec": "prettier",
|
||||
"_where": "/home/kossa/projects/knaeckeboot-tailwind",
|
||||
"author": {
|
||||
"name": "James Long"
|
||||
},
|
||||
"bin": {
|
||||
"prettier": "bin-prettier.js"
|
||||
},
|
||||
"browser": "./standalone.js",
|
||||
"bugs": {
|
||||
"url": "https://github.com/prettier/prettier/issues"
|
||||
},
|
||||
"bundleDependencies": false,
|
||||
"deprecated": false,
|
||||
"description": "Prettier is an opinionated code formatter",
|
||||
"engines": {
|
||||
"node": ">=10.13.0"
|
||||
},
|
||||
"files": [
|
||||
"*.js",
|
||||
"esm/*.mjs"
|
||||
],
|
||||
"funding": "https://github.com/prettier/prettier?sponsor=1",
|
||||
"homepage": "https://prettier.io",
|
||||
"license": "MIT",
|
||||
"main": "./index.js",
|
||||
"name": "prettier",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/prettier/prettier.git"
|
||||
},
|
||||
"unpkg": "./standalone.js",
|
||||
"version": "2.7.1"
|
||||
}
|
||||
+2
File diff suppressed because one or more lines are too long
+27
File diff suppressed because one or more lines are too long
+26
File diff suppressed because one or more lines are too long
+35
File diff suppressed because one or more lines are too long
+27
File diff suppressed because one or more lines are too long
+15
File diff suppressed because one or more lines are too long
+36
File diff suppressed because one or more lines are too long
+76
File diff suppressed because one or more lines are too long
+19
File diff suppressed because one or more lines are too long
+76
File diff suppressed because one or more lines are too long
+280
File diff suppressed because one or more lines are too long
+150
File diff suppressed because one or more lines are too long
+116
File diff suppressed because one or more lines are too long
+8978
File diff suppressed because it is too large
Load Diff
+21
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2017 Alizain Feerasta
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+181
@@ -0,0 +1,181 @@
|
||||
<h1 align="center">
|
||||
<br>
|
||||
<br>
|
||||
<img width="360" src="logo.png" alt="ulid">
|
||||
<br>
|
||||
<br>
|
||||
<br>
|
||||
</h1>
|
||||
|
||||
[](https://travis-ci.org/ulid/javascript) [](https://codecov.io/gh/ulid/javascript)
|
||||
[](https://www.npmjs.com/package/ulid)
|
||||
|
||||
# Universally Unique Lexicographically Sortable Identifier
|
||||
|
||||
UUID can be suboptimal for many uses-cases because:
|
||||
|
||||
- It isn't the most character efficient way of encoding 128 bits of randomness
|
||||
- UUID v1/v2 is impractical in many environments, as it requires access to a unique, stable MAC address
|
||||
- UUID v3/v5 requires a unique seed and produces randomly distributed IDs, which can cause fragmentation in many data structures
|
||||
- UUID v4 provides no other information than randomness which can cause fragmentation in many data structures
|
||||
|
||||
Instead, herein is proposed ULID:
|
||||
|
||||
- 128-bit compatibility with UUID
|
||||
- 1.21e+24 unique ULIDs per millisecond
|
||||
- Lexicographically sortable!
|
||||
- Canonically encoded as a 26 character string, as opposed to the 36 character UUID
|
||||
- Uses Crockford's base32 for better efficiency and readability (5 bits per character)
|
||||
- Case insensitive
|
||||
- No special characters (URL safe)
|
||||
- Monotonic sort order (correctly detects and handles the same millisecond)
|
||||
|
||||
## Installation
|
||||
|
||||
```
|
||||
npm install --save ulid
|
||||
```
|
||||
|
||||
## Import
|
||||
|
||||
**TypeScript, ES6+, Babel, Webpack, Rollup, etc.. environments**
|
||||
```javascript
|
||||
import { ulid } from 'ulid'
|
||||
|
||||
ulid() // 01ARZ3NDEKTSV4RRFFQ69G5FAV
|
||||
```
|
||||
|
||||
**CommonJS environments**
|
||||
```javascript
|
||||
const ULID = require('ulid')
|
||||
|
||||
ULID.ulid()
|
||||
```
|
||||
|
||||
**AMD (RequireJS) environments**
|
||||
```javascript
|
||||
define(['ULID'] , function (ULID) {
|
||||
ULID.ulid()
|
||||
});
|
||||
```
|
||||
|
||||
**Browser**
|
||||
```html
|
||||
<script src="/path/to/ulid.js"></script>
|
||||
<script>
|
||||
ULID.ulid()
|
||||
</script>
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
To generate a ULID, simply run the function!
|
||||
|
||||
```javascript
|
||||
import { ulid } from 'ulid'
|
||||
|
||||
ulid() // 01ARZ3NDEKTSV4RRFFQ69G5FAV
|
||||
```
|
||||
|
||||
### Seed Time
|
||||
|
||||
You can also input a seed time which will consistently give you the same string for the time component. This is useful for migrating to ulid.
|
||||
|
||||
```javascript
|
||||
ulid(1469918176385) // 01ARYZ6S41TSV4RRFFQ69G5FAV
|
||||
```
|
||||
|
||||
### Monotonic ULIDs
|
||||
|
||||
To generate monotonically increasing ULIDs, create a monotonic counter.
|
||||
|
||||
*Note that the same seed time is being passed in for this example to demonstrate its behaviour when generating multiple ULIDs within the same millisecond*
|
||||
|
||||
```javascript
|
||||
import { monotonicFactory } from 'ulid'
|
||||
|
||||
const ulid = monotonicFactory()
|
||||
|
||||
// Strict ordering for the same timestamp, by incrementing the least-significant random bit by 1
|
||||
ulid(150000) // 000XAL6S41ACTAV9WEVGEMMVR8
|
||||
ulid(150000) // 000XAL6S41ACTAV9WEVGEMMVR9
|
||||
ulid(150000) // 000XAL6S41ACTAV9WEVGEMMVRA
|
||||
ulid(150000) // 000XAL6S41ACTAV9WEVGEMMVRB
|
||||
ulid(150000) // 000XAL6S41ACTAV9WEVGEMMVRC
|
||||
|
||||
// Even if a lower timestamp is passed (or generated), it will preserve sort order
|
||||
ulid(100000) // 000XAL6S41ACTAV9WEVGEMMVRD
|
||||
```
|
||||
|
||||
### Pseudo-Random Number Generators
|
||||
|
||||
`ulid` automatically detects a suitable (cryptographically-secure) PRNG. In the browser it will use `crypto.getRandomValues` and on node it will use `crypto.randomBytes`.
|
||||
|
||||
#### Allowing the insecure `Math.random`
|
||||
|
||||
By default, `ulid` will not use `Math.random`, because that is insecure. To allow the use of `Math.random`, you'll have to use `factory` and `detectPrng`.
|
||||
|
||||
```javascript
|
||||
import { factory, detectPrng } from 'ulid'
|
||||
|
||||
const prng = detectPrng(true) // pass `true` to allow insecure
|
||||
const ulid = factory(prng)
|
||||
|
||||
ulid() // 01BXAVRG61YJ5YSBRM51702F6M
|
||||
```
|
||||
|
||||
#### Use your own PRNG
|
||||
|
||||
To use your own pseudo-random number generator, import the factory, and pass it your generator function.
|
||||
|
||||
```javascript
|
||||
import { factory } from 'ulid'
|
||||
import prng from 'somewhere'
|
||||
|
||||
const ulid = factory(prng)
|
||||
|
||||
ulid() // 01BXAVRG61YJ5YSBRM51702F6M
|
||||
```
|
||||
|
||||
You can also pass in a `prng` to the `monotonicFactory` function.
|
||||
|
||||
```javascript
|
||||
import { monotonicFactory } from 'ulid'
|
||||
import prng from 'somewhere'
|
||||
|
||||
const ulid = monotonicFactory(prng)
|
||||
|
||||
ulid() // 01BXAVRG61YJ5YSBRM51702F6M
|
||||
```
|
||||
|
||||
## Implementations in other languages
|
||||
|
||||
Refer to [ulid/spec](https://github.com/ulid/spec)
|
||||
|
||||
## Specification
|
||||
|
||||
Refer to [ulid/spec](https://github.com/ulid/spec)
|
||||
|
||||
## Test Suite
|
||||
|
||||
```
|
||||
npm test
|
||||
```
|
||||
|
||||
## Performance
|
||||
|
||||
```
|
||||
npm run perf
|
||||
```
|
||||
|
||||
```
|
||||
ulid
|
||||
336,331,131 op/s » encodeTime
|
||||
102,041,736 op/s » encodeRandom
|
||||
17,408 op/s » generate
|
||||
|
||||
|
||||
Suites: 1
|
||||
Benches: 3
|
||||
Elapsed: 7,285.75 ms
|
||||
```
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
#! /usr/bin/env node
|
||||
|
||||
var ULID = require('../dist/index.umd.js')
|
||||
process.stdout.write(ULID.ulid())
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
export interface PRNG {
|
||||
(): number;
|
||||
}
|
||||
export interface ULID {
|
||||
(seedTime?: number): string;
|
||||
}
|
||||
export interface LibError extends Error {
|
||||
source: string;
|
||||
}
|
||||
export declare function replaceCharAt(str: string, index: number, char: string): string;
|
||||
export declare function incrementBase32(str: string): string;
|
||||
export declare function randomChar(prng: PRNG): string;
|
||||
export declare function encodeTime(now: number, len: number): string;
|
||||
export declare function encodeRandom(len: number, prng: PRNG): string;
|
||||
export declare function decodeTime(id: string): number;
|
||||
export declare function detectPrng(allowInsecure?: boolean, root?: any): PRNG;
|
||||
export declare function factory(currPrng?: PRNG): ULID;
|
||||
export declare function monotonicFactory(currPrng?: PRNG): ULID;
|
||||
export declare const ulid: ULID;
|
||||
+158
@@ -0,0 +1,158 @@
|
||||
function createError(message) {
|
||||
var err = new Error(message);
|
||||
err.source = "ulid";
|
||||
return err;
|
||||
}
|
||||
// These values should NEVER change. If
|
||||
// they do, we're no longer making ulids!
|
||||
var ENCODING = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; // Crockford's Base32
|
||||
var ENCODING_LEN = ENCODING.length;
|
||||
var TIME_MAX = Math.pow(2, 48) - 1;
|
||||
var TIME_LEN = 10;
|
||||
var RANDOM_LEN = 16;
|
||||
function replaceCharAt(str, index, char) {
|
||||
if (index > str.length - 1) {
|
||||
return str;
|
||||
}
|
||||
return str.substr(0, index) + char + str.substr(index + 1);
|
||||
}
|
||||
function incrementBase32(str) {
|
||||
var done = undefined;
|
||||
var index = str.length;
|
||||
var char = void 0;
|
||||
var charIndex = void 0;
|
||||
var maxCharIndex = ENCODING_LEN - 1;
|
||||
while (!done && index-- >= 0) {
|
||||
char = str[index];
|
||||
charIndex = ENCODING.indexOf(char);
|
||||
if (charIndex === -1) {
|
||||
throw createError("incorrectly encoded string");
|
||||
}
|
||||
if (charIndex === maxCharIndex) {
|
||||
str = replaceCharAt(str, index, ENCODING[0]);
|
||||
continue;
|
||||
}
|
||||
done = replaceCharAt(str, index, ENCODING[charIndex + 1]);
|
||||
}
|
||||
if (typeof done === "string") {
|
||||
return done;
|
||||
}
|
||||
throw createError("cannot increment this string");
|
||||
}
|
||||
function randomChar(prng) {
|
||||
var rand = Math.floor(prng() * ENCODING_LEN);
|
||||
if (rand === ENCODING_LEN) {
|
||||
rand = ENCODING_LEN - 1;
|
||||
}
|
||||
return ENCODING.charAt(rand);
|
||||
}
|
||||
function encodeTime(now, len) {
|
||||
if (isNaN(now)) {
|
||||
throw new Error(now + " must be a number");
|
||||
}
|
||||
if (now > TIME_MAX) {
|
||||
throw createError("cannot encode time greater than " + TIME_MAX);
|
||||
}
|
||||
if (now < 0) {
|
||||
throw createError("time must be positive");
|
||||
}
|
||||
if (Number.isInteger(now) === false) {
|
||||
throw createError("time must be an integer");
|
||||
}
|
||||
var mod = void 0;
|
||||
var str = "";
|
||||
for (; len > 0; len--) {
|
||||
mod = now % ENCODING_LEN;
|
||||
str = ENCODING.charAt(mod) + str;
|
||||
now = (now - mod) / ENCODING_LEN;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
function encodeRandom(len, prng) {
|
||||
var str = "";
|
||||
for (; len > 0; len--) {
|
||||
str = randomChar(prng) + str;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
function decodeTime(id) {
|
||||
if (id.length !== TIME_LEN + RANDOM_LEN) {
|
||||
throw createError("malformed ulid");
|
||||
}
|
||||
var time = id.substr(0, TIME_LEN).split("").reverse().reduce(function (carry, char, index) {
|
||||
var encodingIndex = ENCODING.indexOf(char);
|
||||
if (encodingIndex === -1) {
|
||||
throw createError("invalid character found: " + char);
|
||||
}
|
||||
return carry += encodingIndex * Math.pow(ENCODING_LEN, index);
|
||||
}, 0);
|
||||
if (time > TIME_MAX) {
|
||||
throw createError("malformed ulid, timestamp too large");
|
||||
}
|
||||
return time;
|
||||
}
|
||||
function detectPrng() {
|
||||
var allowInsecure = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
|
||||
var root = arguments[1];
|
||||
|
||||
if (!root) {
|
||||
root = typeof window !== "undefined" ? window : null;
|
||||
}
|
||||
var browserCrypto = root && (root.crypto || root.msCrypto);
|
||||
if (browserCrypto) {
|
||||
return function () {
|
||||
var buffer = new Uint8Array(1);
|
||||
browserCrypto.getRandomValues(buffer);
|
||||
return buffer[0] / 0xff;
|
||||
};
|
||||
} else {
|
||||
try {
|
||||
var nodeCrypto = require("crypto");
|
||||
return function () {
|
||||
return nodeCrypto.randomBytes(1).readUInt8() / 0xff;
|
||||
};
|
||||
} catch (e) {}
|
||||
}
|
||||
if (allowInsecure) {
|
||||
try {
|
||||
console.error("secure crypto unusable, falling back to insecure Math.random()!");
|
||||
} catch (e) {}
|
||||
return function () {
|
||||
return Math.random();
|
||||
};
|
||||
}
|
||||
throw createError("secure crypto unusable, insecure Math.random not allowed");
|
||||
}
|
||||
function factory(currPrng) {
|
||||
if (!currPrng) {
|
||||
currPrng = detectPrng();
|
||||
}
|
||||
return function ulid(seedTime) {
|
||||
if (isNaN(seedTime)) {
|
||||
seedTime = Date.now();
|
||||
}
|
||||
return encodeTime(seedTime, TIME_LEN) + encodeRandom(RANDOM_LEN, currPrng);
|
||||
};
|
||||
}
|
||||
function monotonicFactory(currPrng) {
|
||||
if (!currPrng) {
|
||||
currPrng = detectPrng();
|
||||
}
|
||||
var lastTime = 0;
|
||||
var lastRandom = void 0;
|
||||
return function ulid(seedTime) {
|
||||
if (isNaN(seedTime)) {
|
||||
seedTime = Date.now();
|
||||
}
|
||||
if (seedTime <= lastTime) {
|
||||
var incrementedRandom = lastRandom = incrementBase32(lastRandom);
|
||||
return encodeTime(lastTime, TIME_LEN) + incrementedRandom;
|
||||
}
|
||||
lastTime = seedTime;
|
||||
var newRandom = lastRandom = encodeRandom(RANDOM_LEN, currPrng);
|
||||
return encodeTime(seedTime, TIME_LEN) + newRandom;
|
||||
};
|
||||
}
|
||||
var ulid = factory();
|
||||
|
||||
export { replaceCharAt, incrementBase32, randomChar, encodeTime, encodeRandom, decodeTime, detectPrng, factory, monotonicFactory, ulid };
|
||||
+156
@@ -0,0 +1,156 @@
|
||||
function createError(message) {
|
||||
const err = new Error(message);
|
||||
err.source = "ulid";
|
||||
return err;
|
||||
}
|
||||
// These values should NEVER change. If
|
||||
// they do, we're no longer making ulids!
|
||||
const ENCODING = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; // Crockford's Base32
|
||||
const ENCODING_LEN = ENCODING.length;
|
||||
const TIME_MAX = Math.pow(2, 48) - 1;
|
||||
const TIME_LEN = 10;
|
||||
const RANDOM_LEN = 16;
|
||||
export function replaceCharAt(str, index, char) {
|
||||
if (index > str.length - 1) {
|
||||
return str;
|
||||
}
|
||||
return str.substr(0, index) + char + str.substr(index + 1);
|
||||
}
|
||||
export function incrementBase32(str) {
|
||||
let done = undefined;
|
||||
let index = str.length;
|
||||
let char;
|
||||
let charIndex;
|
||||
const maxCharIndex = ENCODING_LEN - 1;
|
||||
while (!done && index-- >= 0) {
|
||||
char = str[index];
|
||||
charIndex = ENCODING.indexOf(char);
|
||||
if (charIndex === -1) {
|
||||
throw createError("incorrectly encoded string");
|
||||
}
|
||||
if (charIndex === maxCharIndex) {
|
||||
str = replaceCharAt(str, index, ENCODING[0]);
|
||||
continue;
|
||||
}
|
||||
done = replaceCharAt(str, index, ENCODING[charIndex + 1]);
|
||||
}
|
||||
if (typeof done === "string") {
|
||||
return done;
|
||||
}
|
||||
throw createError("cannot increment this string");
|
||||
}
|
||||
export function randomChar(prng) {
|
||||
let rand = Math.floor(prng() * ENCODING_LEN);
|
||||
if (rand === ENCODING_LEN) {
|
||||
rand = ENCODING_LEN - 1;
|
||||
}
|
||||
return ENCODING.charAt(rand);
|
||||
}
|
||||
export function encodeTime(now, len) {
|
||||
if (isNaN(now)) {
|
||||
throw new Error(now + " must be a number");
|
||||
}
|
||||
if (now > TIME_MAX) {
|
||||
throw createError("cannot encode time greater than " + TIME_MAX);
|
||||
}
|
||||
if (now < 0) {
|
||||
throw createError("time must be positive");
|
||||
}
|
||||
if (Number.isInteger(now) === false) {
|
||||
throw createError("time must be an integer");
|
||||
}
|
||||
let mod;
|
||||
let str = "";
|
||||
for (; len > 0; len--) {
|
||||
mod = now % ENCODING_LEN;
|
||||
str = ENCODING.charAt(mod) + str;
|
||||
now = (now - mod) / ENCODING_LEN;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
export function encodeRandom(len, prng) {
|
||||
let str = "";
|
||||
for (; len > 0; len--) {
|
||||
str = randomChar(prng) + str;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
export function decodeTime(id) {
|
||||
if (id.length !== TIME_LEN + RANDOM_LEN) {
|
||||
throw createError("malformed ulid");
|
||||
}
|
||||
var time = id
|
||||
.substr(0, TIME_LEN)
|
||||
.split("")
|
||||
.reverse()
|
||||
.reduce((carry, char, index) => {
|
||||
const encodingIndex = ENCODING.indexOf(char);
|
||||
if (encodingIndex === -1) {
|
||||
throw createError("invalid character found: " + char);
|
||||
}
|
||||
return (carry += encodingIndex * Math.pow(ENCODING_LEN, index));
|
||||
}, 0);
|
||||
if (time > TIME_MAX) {
|
||||
throw createError("malformed ulid, timestamp too large");
|
||||
}
|
||||
return time;
|
||||
}
|
||||
export function detectPrng(allowInsecure = false, root) {
|
||||
if (!root) {
|
||||
root = typeof window !== "undefined" ? window : null;
|
||||
}
|
||||
const browserCrypto = root && (root.crypto || root.msCrypto);
|
||||
if (browserCrypto) {
|
||||
return () => {
|
||||
const buffer = new Uint8Array(1);
|
||||
browserCrypto.getRandomValues(buffer);
|
||||
return buffer[0] / 0xff;
|
||||
};
|
||||
}
|
||||
else {
|
||||
try {
|
||||
const nodeCrypto = require("crypto");
|
||||
return () => nodeCrypto.randomBytes(1).readUInt8() / 0xff;
|
||||
}
|
||||
catch (e) { }
|
||||
}
|
||||
if (allowInsecure) {
|
||||
try {
|
||||
console.error("secure crypto unusable, falling back to insecure Math.random()!");
|
||||
}
|
||||
catch (e) { }
|
||||
return () => Math.random();
|
||||
}
|
||||
throw createError("secure crypto unusable, insecure Math.random not allowed");
|
||||
}
|
||||
export function factory(currPrng) {
|
||||
if (!currPrng) {
|
||||
currPrng = detectPrng();
|
||||
}
|
||||
return function ulid(seedTime) {
|
||||
if (isNaN(seedTime)) {
|
||||
seedTime = Date.now();
|
||||
}
|
||||
return encodeTime(seedTime, TIME_LEN) + encodeRandom(RANDOM_LEN, currPrng);
|
||||
};
|
||||
}
|
||||
export function monotonicFactory(currPrng) {
|
||||
if (!currPrng) {
|
||||
currPrng = detectPrng();
|
||||
}
|
||||
let lastTime = 0;
|
||||
let lastRandom;
|
||||
return function ulid(seedTime) {
|
||||
if (isNaN(seedTime)) {
|
||||
seedTime = Date.now();
|
||||
}
|
||||
if (seedTime <= lastTime) {
|
||||
const incrementedRandom = (lastRandom = incrementBase32(lastRandom));
|
||||
return encodeTime(lastTime, TIME_LEN) + incrementedRandom;
|
||||
}
|
||||
lastTime = seedTime;
|
||||
const newRandom = (lastRandom = encodeRandom(RANDOM_LEN, currPrng));
|
||||
return encodeTime(seedTime, TIME_LEN) + newRandom;
|
||||
};
|
||||
}
|
||||
export const ulid = factory();
|
||||
+177
@@ -0,0 +1,177 @@
|
||||
(function (global, factory) {
|
||||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
||||
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
||||
(factory((global.ULID = {})));
|
||||
}(this, (function (exports) { 'use strict';
|
||||
|
||||
function createError(message) {
|
||||
var err = new Error(message);
|
||||
err.source = "ulid";
|
||||
return err;
|
||||
}
|
||||
// These values should NEVER change. If
|
||||
// they do, we're no longer making ulids!
|
||||
var ENCODING = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"; // Crockford's Base32
|
||||
var ENCODING_LEN = ENCODING.length;
|
||||
var TIME_MAX = Math.pow(2, 48) - 1;
|
||||
var TIME_LEN = 10;
|
||||
var RANDOM_LEN = 16;
|
||||
function replaceCharAt(str, index, char) {
|
||||
if (index > str.length - 1) {
|
||||
return str;
|
||||
}
|
||||
return str.substr(0, index) + char + str.substr(index + 1);
|
||||
}
|
||||
function incrementBase32(str) {
|
||||
var done = undefined;
|
||||
var index = str.length;
|
||||
var char = void 0;
|
||||
var charIndex = void 0;
|
||||
var maxCharIndex = ENCODING_LEN - 1;
|
||||
while (!done && index-- >= 0) {
|
||||
char = str[index];
|
||||
charIndex = ENCODING.indexOf(char);
|
||||
if (charIndex === -1) {
|
||||
throw createError("incorrectly encoded string");
|
||||
}
|
||||
if (charIndex === maxCharIndex) {
|
||||
str = replaceCharAt(str, index, ENCODING[0]);
|
||||
continue;
|
||||
}
|
||||
done = replaceCharAt(str, index, ENCODING[charIndex + 1]);
|
||||
}
|
||||
if (typeof done === "string") {
|
||||
return done;
|
||||
}
|
||||
throw createError("cannot increment this string");
|
||||
}
|
||||
function randomChar(prng) {
|
||||
var rand = Math.floor(prng() * ENCODING_LEN);
|
||||
if (rand === ENCODING_LEN) {
|
||||
rand = ENCODING_LEN - 1;
|
||||
}
|
||||
return ENCODING.charAt(rand);
|
||||
}
|
||||
function encodeTime(now, len) {
|
||||
if (isNaN(now)) {
|
||||
throw new Error(now + " must be a number");
|
||||
}
|
||||
if (now > TIME_MAX) {
|
||||
throw createError("cannot encode time greater than " + TIME_MAX);
|
||||
}
|
||||
if (now < 0) {
|
||||
throw createError("time must be positive");
|
||||
}
|
||||
if (Number.isInteger(now) === false) {
|
||||
throw createError("time must be an integer");
|
||||
}
|
||||
var mod = void 0;
|
||||
var str = "";
|
||||
for (; len > 0; len--) {
|
||||
mod = now % ENCODING_LEN;
|
||||
str = ENCODING.charAt(mod) + str;
|
||||
now = (now - mod) / ENCODING_LEN;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
function encodeRandom(len, prng) {
|
||||
var str = "";
|
||||
for (; len > 0; len--) {
|
||||
str = randomChar(prng) + str;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
function decodeTime(id) {
|
||||
if (id.length !== TIME_LEN + RANDOM_LEN) {
|
||||
throw createError("malformed ulid");
|
||||
}
|
||||
var time = id.substr(0, TIME_LEN).split("").reverse().reduce(function (carry, char, index) {
|
||||
var encodingIndex = ENCODING.indexOf(char);
|
||||
if (encodingIndex === -1) {
|
||||
throw createError("invalid character found: " + char);
|
||||
}
|
||||
return carry += encodingIndex * Math.pow(ENCODING_LEN, index);
|
||||
}, 0);
|
||||
if (time > TIME_MAX) {
|
||||
throw createError("malformed ulid, timestamp too large");
|
||||
}
|
||||
return time;
|
||||
}
|
||||
function detectPrng() {
|
||||
var allowInsecure = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
|
||||
var root = arguments[1];
|
||||
|
||||
if (!root) {
|
||||
root = typeof window !== "undefined" ? window : null;
|
||||
}
|
||||
var browserCrypto = root && (root.crypto || root.msCrypto);
|
||||
if (browserCrypto) {
|
||||
return function () {
|
||||
var buffer = new Uint8Array(1);
|
||||
browserCrypto.getRandomValues(buffer);
|
||||
return buffer[0] / 0xff;
|
||||
};
|
||||
} else {
|
||||
try {
|
||||
var nodeCrypto = require("crypto");
|
||||
return function () {
|
||||
return nodeCrypto.randomBytes(1).readUInt8() / 0xff;
|
||||
};
|
||||
} catch (e) {}
|
||||
}
|
||||
if (allowInsecure) {
|
||||
try {
|
||||
console.error("secure crypto unusable, falling back to insecure Math.random()!");
|
||||
} catch (e) {}
|
||||
return function () {
|
||||
return Math.random();
|
||||
};
|
||||
}
|
||||
throw createError("secure crypto unusable, insecure Math.random not allowed");
|
||||
}
|
||||
function factory(currPrng) {
|
||||
if (!currPrng) {
|
||||
currPrng = detectPrng();
|
||||
}
|
||||
return function ulid(seedTime) {
|
||||
if (isNaN(seedTime)) {
|
||||
seedTime = Date.now();
|
||||
}
|
||||
return encodeTime(seedTime, TIME_LEN) + encodeRandom(RANDOM_LEN, currPrng);
|
||||
};
|
||||
}
|
||||
function monotonicFactory(currPrng) {
|
||||
if (!currPrng) {
|
||||
currPrng = detectPrng();
|
||||
}
|
||||
var lastTime = 0;
|
||||
var lastRandom = void 0;
|
||||
return function ulid(seedTime) {
|
||||
if (isNaN(seedTime)) {
|
||||
seedTime = Date.now();
|
||||
}
|
||||
if (seedTime <= lastTime) {
|
||||
var incrementedRandom = lastRandom = incrementBase32(lastRandom);
|
||||
return encodeTime(lastTime, TIME_LEN) + incrementedRandom;
|
||||
}
|
||||
lastTime = seedTime;
|
||||
var newRandom = lastRandom = encodeRandom(RANDOM_LEN, currPrng);
|
||||
return encodeTime(seedTime, TIME_LEN) + newRandom;
|
||||
};
|
||||
}
|
||||
var ulid = factory();
|
||||
|
||||
exports.replaceCharAt = replaceCharAt;
|
||||
exports.incrementBase32 = incrementBase32;
|
||||
exports.randomChar = randomChar;
|
||||
exports.encodeTime = encodeTime;
|
||||
exports.encodeRandom = encodeRandom;
|
||||
exports.decodeTime = decodeTime;
|
||||
exports.detectPrng = detectPrng;
|
||||
exports.factory = factory;
|
||||
exports.monotonicFactory = monotonicFactory;
|
||||
exports.ulid = ulid;
|
||||
|
||||
Object.defineProperty(exports, '__esModule', { value: true });
|
||||
|
||||
})));
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
{
|
||||
"_from": "ulid@^2.3.0",
|
||||
"_id": "ulid@2.3.0",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-keqHubrlpvT6G2wH0OEfSW4mquYRcbe/J8NMmveoQOjUqmo+hXtO+ORCpWhdbZ7k72UtY61BL7haGxW6enBnjw==",
|
||||
"_location": "/ulid",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "range",
|
||||
"registry": true,
|
||||
"raw": "ulid@^2.3.0",
|
||||
"name": "ulid",
|
||||
"escapedName": "ulid",
|
||||
"rawSpec": "^2.3.0",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "^2.3.0"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/prettier-plugin-go-template"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/ulid/-/ulid-2.3.0.tgz",
|
||||
"_shasum": "93063522771a9774121a84d126ecd3eb9804071f",
|
||||
"_spec": "ulid@^2.3.0",
|
||||
"_where": "/home/kossa/projects/knaeckeboot-tailwind/node_modules/prettier-plugin-go-template",
|
||||
"author": {
|
||||
"name": "Alizain Feerasta"
|
||||
},
|
||||
"bin": {
|
||||
"ulid": "bin/cli.js"
|
||||
},
|
||||
"browser": {
|
||||
"crypto": "./stubs/crypto.js"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/ulid/javascript/issues"
|
||||
},
|
||||
"bundleDependencies": false,
|
||||
"deprecated": false,
|
||||
"description": "A universally-unique, lexicographically-sortable, identifier generator",
|
||||
"devDependencies": {
|
||||
"@types/node": "^8.0.47",
|
||||
"babel-core": "^6.26.0",
|
||||
"babel-plugin-external-helpers": "^6.22.0",
|
||||
"babel-preset-env": "^1.6.1",
|
||||
"istanbul": "^0.4.4",
|
||||
"lolex": "^2.1.3",
|
||||
"matcha": "^0.7.0",
|
||||
"mocha": "^2.5.3",
|
||||
"rollup": "^0.50.0",
|
||||
"rollup-plugin-babel": "^3.0.2",
|
||||
"rollup-plugin-typescript": "^0.8.1",
|
||||
"typedoc": "^0.9.0",
|
||||
"typescript": "^2.5.3"
|
||||
},
|
||||
"esnext": "./dist/index.js",
|
||||
"files": [
|
||||
"bin",
|
||||
"dist",
|
||||
"stubs"
|
||||
],
|
||||
"homepage": "https://github.com/ulid/javascript#readme",
|
||||
"license": "MIT",
|
||||
"main": "./dist/index.umd.js",
|
||||
"module": "./dist/index.esm.js",
|
||||
"name": "ulid",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/ulid/javascript.git"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "npm run ts && npm run rollup",
|
||||
"perf": "matcha perf.js",
|
||||
"rollup": "rollup -c",
|
||||
"test": "istanbul cover node_modules/mocha/bin/_mocha -- -R spec",
|
||||
"ts": "tsc -p ."
|
||||
},
|
||||
"types": "./dist/index.d.ts",
|
||||
"version": "2.3.0"
|
||||
}
|
||||
Reference in New Issue
Block a user