Skip to content

Commit

Permalink
Add code
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdacasserole committed Sep 9, 2022
1 parent 6be82e9 commit 07390ac
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ typings/

# Nuxt.js build / generate output
.nuxt
dist
# dist

# Gatsby files
.cache/
Expand Down
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,33 @@
# pep440conv
Simple stdin/stdout-based utilities for converting to and from PEP-440 semver strings.

## Requirements

- Node.js 14 or newer

## Installation

The pep440conv utility can be installed using `npm`:

```sh
npm -g pep440conv
```

## Usage

Usage is very straightforward. Once the packge is installed `frompep440` and `topep440` become available from the
command line:

```bash
$ # Converting from a regular semver string to to PEP-440.
$ echo '1.0.0-rc.1' | topep440
1.0.0-rc1

$ # Converting from PEP-440 to a regular semver string.
$ echo '1.0.0-rc1' | frompep440
1.0.0-rc.1
```

## License

[MIT](LICENSE) © [lambdacasserole](https://github.com/lambdacasserole).
40 changes: 40 additions & 0 deletions fromPep440.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env node

import getStdin from 'get-stdin';


/**
* Transforms a PEP-440 compatible version string token into a regular semver string token.
*
* @param {string} token the token to transform
* @returns {string} the transformed token
*/
function fromPep440Token(token) {
const chars = []
let dotted = false
token.split('').forEach((c)=> {
if (/[0-9]/.test(c) && !dotted) {
chars.push('.');
dotted = true;
}
chars.push(c);
})
return chars.join('');
}

/**
* Transforms a PEP-440 compatible version string into a regular semver string.
*
* @param {string} pepVersionStr the string to transform
* @returns {string} the transformed string
*/
function fromPep440(pepVersionStr) {
const tokens = pepVersionStr.split('-');
return [tokens[0], ...tokens.slice(1).map(fromPep440Token)].join('-');
}


(async () => {
const stdin = await getStdin();
console.log(fromPep440(stdin));
})();
37 changes: 37 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "pep440conv",
"version": "1.0.0",
"description": "Simple stdin/stdout-based utilities for converting to and from PEP-440 semver strings.",
"type": "module",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/lambdacasserole/incv.git"
},
"keywords": [
"version",
"pep",
"440",
"python",
"semver"
],
"author": "Saul Johnson <saul.a.johnson@gmail.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/lambdacasserole/pep440conv/issues"
},
"homepage": "https://github.com/lambdacasserole/pep440conv#readme",
"bin": {
"topep440": "./toPep440.js",
"frompep440": "./fromPep440.js"
},
"dependencies": {
"get-stdin": "^9.0.0"
}
}
21 changes: 21 additions & 0 deletions toPep440.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env node

import getStdin from 'get-stdin';


/**
* Transforms a regular semver string to a PEP-440 compatible version string.
*
* @param {string} semverStr the string to transform
* @returns {string} the transformed string
*/
function toPep440(semverStr) {
const tokens = semverStr.split('-');
return [tokens[0], ...tokens.slice(1).map((t) => t.replace('.', ''))].join('-');
}


(async () => {
const stdin = await getStdin();
console.log(toPep440(stdin));
})();

0 comments on commit 07390ac

Please sign in to comment.