Skip to content

Commit c6525f7

Browse files
lgeigerrgbkrk
authored andcommitted
Switch to flow (#27)
1 parent 29080d8 commit c6525f7

File tree

3 files changed

+47
-16
lines changed

3 files changed

+47
-16
lines changed

.flowconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[ignore]
2+
3+
[include]
4+
5+
[libs]
6+
7+
[options]

package.json

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
"scripts": {
77
"prebuild": "mkdirp lib/ && npm run clean",
88
"prepublish": "npm run build",
9-
"test": "mocha --compilers js:babel-core/register 'test/**/*.js'",
9+
"test": "npm run test:unit && npm run test:flow",
10+
"test:flow": "flow",
11+
"test:unit": "mocha --compilers js:babel-core/register 'test/**/*.js'",
1012
"test:watch": "npm run test -- --watch",
11-
"build": "npm run build:es5",
13+
"build": "npm run build:es5 && npm run build:flow",
14+
"build:flow": "flow-copy-source -v -i '**/tests/**' src lib",
1215
"build:es5": "babel src --out-dir lib/ --source-maps",
1316
"clean": "rimraf lib/*"
1417
},
@@ -25,6 +28,9 @@
2528
"es2015",
2629
"stage-3",
2730
"react"
31+
],
32+
"plugins": [
33+
"transform-flow-strip-types"
2834
]
2935
},
3036
"author": "Kyle Kelley <[email protected]>",
@@ -34,23 +40,24 @@
3440
"escape-carriage": "^1.0.1"
3541
},
3642
"peerDependencies": {
37-
"prop-types": "^15.5.9",
3843
"react": "^15.0.0-0"
3944
},
4045
"devDependencies": {
4146
"babel-cli": "^6.8.0",
4247
"babel-core": "^6.8.0",
48+
"babel-plugin-transform-flow-strip-types": "^6.22.0",
4349
"babel-preset-es2015": "^6.6.0",
4450
"babel-preset-react": "^6.5.0",
4551
"babel-preset-stage-3": "^6.22.0",
4652
"chai": "^3.5.0",
4753
"enzyme": "^2.2.0",
54+
"flow-bin": "^0.46.0",
55+
"flow-copy-source": "^1.1.0",
4856
"mkdirp": "0.5.1",
4957
"mocha": "^3.2.0",
5058
"react": "^15.0.1",
5159
"react-addons-test-utils": "^15.0.1",
5260
"react-dom": "^15.0.1",
53-
"rimraf": "^2.5.2",
54-
"should": "^11.1.2"
61+
"rimraf": "^2.5.2"
5562
}
5663
}

src/index.js

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
1+
/* @flow */
2+
13
const React = require('react');
2-
const PropTypes = require('prop-types');
34
const Anser = require('anser');
45
const escapeCarriageReturn = require('escape-carriage');
56

7+
type AnserJsonEntry = {
8+
content: string,
9+
fg: string,
10+
bg: string,
11+
fg_truecolor: string,
12+
bg_truecolor: string,
13+
clearLine: boolean,
14+
was_processed: boolean,
15+
isEmpty: () => boolean
16+
};
17+
18+
type AnserJson = Array<AnserJsonEntry>;
19+
20+
type AnsiBundle = {
21+
content: string,
22+
style: {
23+
color?: string,
24+
backgroundColor?: string
25+
}
26+
};
27+
628
/**
729
* ansiToJson
830
* Convert ANSI strings into JSON output.
@@ -12,15 +34,15 @@ const escapeCarriageReturn = require('escape-carriage');
1234
* @param {String} input The input string.
1335
* @return {Array} The parsed input.
1436
*/
15-
function ansiToJSON(input) {
37+
function ansiToJSON(input: string): AnserJson {
1638
input = escapeCarriageReturn(input);
1739
return Anser.ansiToJson(input, {
1840
json: true,
1941
remove_empty: true,
2042
});
2143
}
2244

23-
function ansiJSONtoStyleBundle(ansiBundle) {
45+
function ansiJSONtoStyleBundle(ansiBundle: AnserJsonEntry): AnsiBundle {
2446
const style = {};
2547
if (ansiBundle.bg) {
2648
style.backgroundColor = `rgb(${ansiBundle.bg})`;
@@ -34,11 +56,11 @@ function ansiJSONtoStyleBundle(ansiBundle) {
3456
};
3557
}
3658

37-
function ansiToInlineStyle(text) {
59+
function ansiToInlineStyle(text: string): Array<AnsiBundle> {
3860
return ansiToJSON(text).map(ansiJSONtoStyleBundle);
3961
}
4062

41-
function linkifyBundle(bundle) {
63+
function linkifyBundle(bundle: AnsiBundle) {
4264
return {
4365
...bundle,
4466
content: bundle.content.split(' ').reduce((result, word, index) => [
@@ -68,7 +90,7 @@ function inlineBundleToReact(bundle, key) {
6890
}, bundle.content);
6991
}
7092

71-
function Ansi(props) {
93+
function Ansi(props: {children: string, className?: string}) {
7294
return React.createElement(
7395
'code',
7496
{className: props.className},
@@ -80,9 +102,4 @@ function Ansi(props) {
80102
);
81103
}
82104

83-
Ansi.propTypes = {
84-
children: PropTypes.string,
85-
className: PropTypes.string
86-
};
87-
88105
module.exports = Ansi;

0 commit comments

Comments
 (0)