Skip to content

Commit e5d51da

Browse files
committed
Use plain objects for file formatters.
1 parent 35d2466 commit e5d51da

File tree

6 files changed

+16
-25
lines changed

6 files changed

+16
-25
lines changed

src/formats/__tests__/frontmatter.spec.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import Frontmatter from '../frontmatter';
1+
import FrontmatterFormatter from '../frontmatter';
22

33
jest.mock("../../valueObjects/AssetProxy.js");
44

5-
const FrontmatterFormatter = new Frontmatter();
6-
75
describe('Frontmatter', () => {
86
it('should parse YAML with --- delimiters', () => {
97
expect(

src/formats/formats.js

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { partial } from 'lodash';
2-
import YAML from './yaml';
3-
import TOML from './toml';
4-
import JSONFormatter from './json';
5-
import Frontmatter from './frontmatter';
2+
import yamlFormatter from './yaml';
3+
import tomlFormatter from './toml';
4+
import jsonFormatter from './json';
5+
import FrontmatterFormatter from './frontmatter';
66

77
export const formatToExtension = format => ({
88
markdown: 'md',
@@ -11,11 +11,6 @@ export const formatToExtension = format => ({
1111
html: 'html',
1212
}[format]);
1313

14-
const yamlFormatter = new YAML();
15-
const tomlFormatter = new TOML();
16-
const jsonFormatter = new JSONFormatter();
17-
const FrontmatterFormatter = new Frontmatter();
18-
1914
function formatByType(type) {
2015
// Right now the only type is "editorialWorkflow" and
2116
// we always returns the same format

src/formats/frontmatter.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import matter from 'gray-matter';
2-
import TOML from './toml';
3-
import YAML from './yaml';
4-
5-
const tomlFormatter = new TOML();
2+
import tomlFormatter from './toml';
3+
import yamlFormatter from './yaml';
64

75
const parsers = {
86
toml: input => tomlFormatter.fromFile(null, input),
@@ -37,22 +35,22 @@ function inferFrontmatterFormat(str) {
3735
}
3836
}
3937

40-
export default class Frontmatter {
38+
export default {
4139
fromFile(collectionOrEntity, content) {
4240
const result = matter(content, { engines: parsers, ...inferFrontmatterFormat(content) });
4341
return {
4442
...result.data,
4543
body: result.content,
4644
};
47-
}
45+
},
4846

4947
toFile(collectionOrEntity, data, sortedKeys) {
5048
const { body, ...meta } = data;
5149

5250
// always stringify to YAML
5351
const parser = {
5452
stringify(metadata) {
55-
return new YAML().toFile(collectionOrEntity, metadata, sortedKeys);
53+
return yamlFormatter.toFile(collectionOrEntity, metadata, sortedKeys);
5654
},
5755
};
5856
return matter.stringify(body, meta, { language: "yaml", delimiters: "---", engines: { yaml: parser } });

src/formats/json.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
export default class JSONFormatter {
1+
export default {
22
fromFile(collectionOrEntity, content) {
33
return JSON.parse(content);
4-
}
4+
},
55

66
toFile(collectionOrEntity, data) {
77
return JSON.stringify(data);

src/formats/toml.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ const outputReplacer = (key, value) => {
1515
return false;
1616
};
1717

18-
export default class TOML {
18+
export default {
1919
fromFile(collectionOrEntity, content) {
2020
return toml.parse(content);
21-
}
21+
},
2222

2323
toFile(collectionOrEntity, data, sortedKeys = []) {
2424
return tomlify.toToml(data, { replace: outputReplacer, sort: sortKeys(sortedKeys) });

src/formats/yaml.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ const OutputSchema = new yaml.Schema({
3636
explicit: yaml.DEFAULT_SAFE_SCHEMA.explicit,
3737
});
3838

39-
export default class YAML {
39+
export default {
4040
fromFile(collectionOrEntity, content) {
4141
return yaml.safeLoad(content);
42-
}
42+
},
4343

4444
toFile(collectionOrEntity, data, sortedKeys = []) {
4545
return yaml.safeDump(data, { schema: OutputSchema, sortKeys: sortKeys(sortedKeys) });

0 commit comments

Comments
 (0)