Skip to content

Commit

Permalink
add normalizedYear and numberOfCharacters to generated JSON
Browse files Browse the repository at this point in the history
resolves #13
  • Loading branch information
cmil committed Oct 23, 2020
1 parent a28c022 commit 5e912a1
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*

data.json
src/data.json
public/data.json
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"typescript": "~3.7.2"
},
"scripts": {
"json": "js-yaml ./data.yaml > ./src/data.json && js-yaml ./data.yaml > ./public/data.json",
"json": "ts-node -O '{\"isolatedModules\":false,\"module\":\"CommonJS\"}' yml2json.ts && cp ./data.json ./src/ && cp ./data.json ./public/",
"start": "yarn json && react-scripts start",
"build": "yarn json && react-scripts build",
"test": "yarn json && react-scripts test",
Expand Down
2 changes: 1 addition & 1 deletion src/components/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {FontAwesomeIcon} from '@fortawesome/react-fontawesome';
import Authors from './Authors';
import Years from './Years';
import {CastMember, Play} from '../types';
import data from '../plays';
import data from '../data.json';

const groupIcon = <FontAwesomeIcon icon="users" size="sm" title="Group"/>;

Expand Down
7 changes: 2 additions & 5 deletions src/components/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import {Link} from 'react-router-dom';
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome';
import Authors from './Authors';
import {Play} from '../types';
import data from '../plays';

const dataURI = 'data:text/json;base64,' +
btoa(unescape(encodeURIComponent(JSON.stringify(data))));
import data from '../data.json';

function formatAuthor (_: string, play: Play) {
return <Authors authors={play.authors || []}/>;
Expand Down Expand Up @@ -106,7 +103,7 @@ function Table () {
<span className="counter">
Database currently containing {data.length} one-act plays
{' '}
<a href={dataURI} className="download" download="einakter.json" >
<a href="data.json" className="download" download="einakter.json" >
<FontAwesomeIcon icon="download" title="Download JSON"/>
</a>
</span>
Expand Down
18 changes: 4 additions & 14 deletions src/plays.ts → src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {Play} from './types';
import data from './data.json';

function normalizeYear (play: Play) {
export function normalizeYear (play: Play) {
const {premiered: p, printed, created} = play;
const premiered: number = parseInt(p as string);
const published = (premiered && printed)
Expand All @@ -16,22 +15,13 @@ function normalizeYear (play: Play) {
}

return year;
}
};

function countCharacters(play: Play) {
export function countCharacters (play: Play) {
const {cast} = play;
if (!cast) return undefined;
return cast.reduce((num, item) => {
const n = item.group ? item.group.length : 1;
return num + n;
}, 0)
}

const plays = data.map((p: Play) => {
const normalizedYear = normalizeYear(p);
const numberOfCharacters = countCharacters(p);
const authors = p.author ? [p.author] : p.authors || [];
return {...p, authors, normalizedYear, numberOfCharacters}
});

export default plays;
};
20 changes: 20 additions & 0 deletions yml2json.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {safeLoadAll} from 'js-yaml';
import {readFileSync, writeFileSync} from 'fs';
import {Play} from './src/types';
import {normalizeYear, countCharacters} from './src/utils';

let data: Play[] = [];
try {
data = safeLoadAll(readFileSync('./data.yaml', 'utf8'));
} catch (error) {
console.log(error);
}

const plays = data.map((p: Play) => {
const normalizedYear = normalizeYear(p);
const numberOfCharacters = countCharacters(p);
const authors = p.author ? [p.author] : p.authors || [];
return {...p, authors, normalizedYear, numberOfCharacters}
});

writeFileSync('./data.json', JSON.stringify(plays));

0 comments on commit 5e912a1

Please sign in to comment.