Skip to content

Commit

Permalink
feat(yaml): provide file id to transform function (#615)
Browse files Browse the repository at this point in the history
  • Loading branch information
janosh authored and NotWoods committed Oct 22, 2020
1 parent dbececf commit 74d5dab
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/yaml/README.md
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
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
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

0 comments on commit 74d5dab

Please sign in to comment.