-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Templatize readme * Fix order of build
- Loading branch information
Showing
6 changed files
with
183 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Safe Units | ||
|
||
[![NPM Version](https://img.shields.io/npm/v/safe-units.svg)](https://www.npmjs.com/package/safe-units) [![MIT License](https://img.shields.io/npm/l/safe-units.svg)](https://github.com/jscheiny/safe-units/blob/master/LICENSE) | ||
|
||
Safe Units is a type safe library for using units of measurement in TypeScript. Safe Units provides an implementation of an SI based unit system but is flexible enough to allow users to create their own unit systems which can be independent or can interoperate with the built-in units. Users can also make unit systems for any numeric type they'd like not just the JavaScript `number` type. This library requires TypeScript 3.2 or higher. | ||
|
||
example: intro.ts | ||
|
||
## Features | ||
|
||
⭐ Compile-time unit arithmetic for typesafe dimensional analysis (with exponents between -5 and +5)! | ||
|
||
⭐ Large library of predefined units including SI (with prefixes), Imperial, and US customary units! | ||
|
||
⭐ Ability to add your own unit system that can work with built-in units! | ||
|
||
⭐ Long build times & cryptic error messages! | ||
|
||
## Prerequisites | ||
|
||
Safe units is written in TypeScript and should be consumed by TypeScript users to take full advantage of what it provides. In addition you will need the following: | ||
|
||
- [TypeScript](http://www.typescriptlang.org/) 3.2 or later | ||
- [Strict null checks](https://www.typescriptlang.org/docs/handbook/compiler-options.html) enabled for your project | ||
|
||
## Installation | ||
|
||
``` | ||
npm install safe-units | ||
``` | ||
|
||
or | ||
|
||
``` | ||
yarn add safe-units | ||
``` | ||
|
||
## Examples | ||
|
||
### Unit Arithmetic | ||
|
||
example: introUnitArithmetic.ts | ||
|
||
### Type Errors | ||
|
||
example: introTypeErrors.ts | ||
|
||
### Naming Units | ||
|
||
example: introNamingUnits.ts | ||
|
||
### Deriving Quantities | ||
|
||
example: introDerivingQuantities.ts | ||
|
||
### Defining Unit Systems | ||
|
||
example: introUnitSystem.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { existsSync, readFileSync } from "fs"; | ||
import { join } from "path"; | ||
import { exit } from "process"; | ||
|
||
const EXAMPLE_START_REGEX = /^\/\/\s+start\s*/i; | ||
const EXAMPLE_END_REGX = /^\/\/\s+end\s*/i; | ||
|
||
export function readExample(fileName: string) { | ||
const examplePath = join("docs", "examples", fileName.trim()); | ||
if (!existsSync(examplePath)) { | ||
console.error(`Example file not found: ${examplePath}`); | ||
exit(1); | ||
} | ||
|
||
const contents = readFileSync(examplePath, "utf-8"); | ||
|
||
const lines = contents.split("\n"); | ||
const startLine = lines.findIndex(line => EXAMPLE_START_REGEX.test(line)); | ||
const endLine = lines.findIndex(line => EXAMPLE_END_REGX.test(line)); | ||
|
||
if (startLine === -1 || endLine === -1) { | ||
return lines; | ||
} | ||
|
||
return lines.slice(startLine + 1, endLine); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { existsSync, readFileSync, writeFileSync } from "fs"; | ||
import { join } from "path"; | ||
import { readExample } from "./example"; | ||
import { exit } from "process"; | ||
|
||
const EXAMPLE_INLINE_REGEX = /^example:\s*(\w+\.ts)/i; | ||
|
||
const templatePath = join("docs", "readme", "readme.template.md"); | ||
if (!existsSync(templatePath)) { | ||
console.error(`Template file not found: ${templatePath}`); | ||
exit(1); | ||
} | ||
|
||
const file = readFileSync(templatePath, "utf-8"); | ||
const templateLines = file.split("\n"); | ||
const resolvedLines = templateLines.flatMap(line => { | ||
const match = EXAMPLE_INLINE_REGEX.exec(line); | ||
if (match == null) { | ||
return [line]; | ||
} | ||
|
||
const fileName = match[1]; | ||
return ["```ts", ...readExample(fileName), "```"]; | ||
}); | ||
const expectedReadme = resolvedLines.join("\n"); | ||
|
||
if (process.argv.length >= 3 && process.argv[2] === "--check") { | ||
const existingReadme = readFileSync("README.md", "utf-8"); | ||
if (existingReadme !== expectedReadme) { | ||
console.error("README.md is out of date. Run `yarn readme` to update it."); | ||
exit(1); | ||
} | ||
} else { | ||
writeFileSync("README.md", expectedReadme); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters