Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/yaml/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ A function which can optionally mutate parsed YAML. The function should return t

```js
yaml({
transform(data) {
if (Array.isArray(data)) {
transform(data, filePath) {
if (Array.isArray(data) && filePath === './my-file.yml') {
return data.filter(character => !character.batman);
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/yaml/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const defaults = {
};
const ext = /\.ya?ml$/;

export default function yamll(opts = {}) {
export default function yaml(opts = {}) {
const options = Object.assign({}, defaults, opts);
const { documentMode, safe } = options;
const filter = createFilter(options.include, options.exclude);
Expand All @@ -35,7 +35,7 @@ export default function yamll(opts = {}) {
let data = loadMethod(content);

if (typeof options.transform === 'function') {
const result = options.transform(data);
const result = options.transform(data, id);
// eslint-disable-next-line no-undefined
if (result !== undefined) {
data = result;
Expand Down
5 changes: 4 additions & 1 deletion packages/yaml/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ test('resolves extensionless imports in conjunction with nodeResolve plugin', as
});

test('applies the optional transform method to parsed YAML', async (t) => {
const transform = (data) => {
const transform = (data, filePath) => {
// check that transformer is passed a correct file path
t.true(typeof filePath === 'string' && filePath.endsWith('.yaml'), filePath);
if (Array.isArray(data)) {
t.true(filePath.endsWith('array.yaml'), filePath);
return data.filter((datum) => !datum.private);
}
Object.keys(data).forEach((key) => {
Expand Down