Skip to content

Commit 74d5dab

Browse files
janoshNotWoods
authored andcommitted
feat(yaml): provide file id to transform function (#615)
1 parent dbececf commit 74d5dab

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

packages/yaml/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ A function which can optionally mutate parsed YAML. The function should return t
9090

9191
```js
9292
yaml({
93-
transform(data) {
94-
if (Array.isArray(data)) {
93+
transform(data, filePath) {
94+
if (Array.isArray(data) && filePath === './my-file.yml') {
9595
return data.filter(character => !character.batman);
9696
}
9797
}

packages/yaml/src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const defaults = {
99
};
1010
const ext = /\.ya?ml$/;
1111

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

3737
if (typeof options.transform === 'function') {
38-
const result = options.transform(data);
38+
const result = options.transform(data, id);
3939
// eslint-disable-next-line no-undefined
4040
if (result !== undefined) {
4141
data = result;

packages/yaml/test/test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,11 @@ test('resolves extensionless imports in conjunction with nodeResolve plugin', as
5959
});
6060

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

0 commit comments

Comments
 (0)