Coder Social home page Coder Social logo

yawn-yaml's Introduction

YAWN YAML 🥱

YAML parser that preserves comments and styling

Live Demo

Usage

import YAWN from 'yawn-yaml';

let str = `
# my comment
value: 1 # the value is here!
`;

let yawn = new YAWN(str);

// update the `json` property
yawn.json = { value: 2 };


// value in `yawn.yaml` is now changed.
// with comments and styling preserved.
console.log(yawn.yaml); // =>
// # my comment
// value: 2 # the value is here!

Installation

Use npm or Bower to install yawn-yaml package

npm install --save yawn-yaml
yarn add yawm-yaml

Development

To install dependencies run:

yarn install

To run tests continuously and watch for changes

yarn start

To run the test run:

yarn test

License

MIT

yawn-yaml's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

yawn-yaml's Issues

Should be able to add an attribute after expanding an array

Executed:

const Yawn = require('yawn-yaml/cjs')

const input = `
a:
  - b
c:
  d: e
`
const original = new Yawn(input)
const json = original.json

json.a.push('z')

json.c.f = {}
json.c.f.g = 'h'

console.log(JSON.stringify(json, null, 2))

original.json = json
console.log(original.yaml)

Expected:

{
  "a": [
    "b",
    "z"
  ],
  "c": {
    "d": "e",
    "f": {
      "g": "h"
    }
  }
}

a:
  - b
  - z
c:
  d: e
  f:
    g: h

Observed:

{
  "a": [
    "b",
    "z"
  ],
  "c": {
    "d": "e",
    "f": {
      "g": "h"
    }
  }
}

~/node_modules/yaml-js/lib/scanner.js:504
throw new exports.ScannerError(null, null, 'mapping values are not allowed here', this.get_mark());
^
mapping values are not allowed here
on line 8, column 12
at ScannerError.YAMLError [as constructor] (~/node_modules/yaml-js/lib/errors.js:70:46)
at ScannerError.MarkedYAMLError [as constructor] (~/node_modules/yaml-js/lib/errors.js:90:45)
at new ScannerError (~/node_modules/yaml-js/lib/scanner.js:23:49)
at Constructor.__dirname.Scanner.Scanner.fetch_value (~/node_modules/yaml-js/lib/scanner.js:504:19)
at Constructor.__dirname.Scanner.Scanner.fetch_more_tokens (~/node_modules/yaml-js/lib/scanner.js:214:21)
at Constructor.__dirname.Scanner.Scanner.check_token (~/node_modules/yaml-js/lib/scanner.js:115:14)
at Constructor.__dirname.Parser.Parser.parse_block_mapping_key (~/node_modules/yaml-js/lib/parser.js:421:16)
at Constructor.__dirname.Parser.Parser.check_event (~/node_modules/yaml-js/lib/parser.js:61:48)
at Constructor.__dirname.Composer.Composer.compose_mapping_node (~/node_modules/yaml-js/lib/composer.js:248:20)
at Constructor.__dirname.Composer.Composer.compose_node (~/node_modules/yaml-js/lib/composer.js:160:21)

Process finished with exit code 1

Remove window references from the dist/yawn.js package

I used your library with node, and at import, it throws an error.

My usage is like:
import YAWN from 'yawn-yaml';

..and I get an exception like:

.../node_modules/yawn-yaml/dist/yawn.js:97
var _yamlJs = (window.yaml);
              ^

ReferenceError: window is not defined
    at Object.3../error.js (.../node_modules/yawn-yaml/dist/yawn.js:97:15)
    at s (.../node_modules/yawn-yaml/dist/yawn.js:1:618)
    at e (.../node_modules/yawn-yaml/dist/yawn.js:1:789)
    at .../node_modules/yawn-yaml/dist/yawn.js:1:807
    at .../node_modules/yawn-yaml/dist/yawn.js:1:88
    at Object.<anonymous> (.../node_modules/yawn-yaml/dist/yawn.js:1:320)
    at Module._compile (internal/modules/cjs/loader.js:1200:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
    at Module.load (internal/modules/cjs/loader.js:1049:32)
    at Function.Module._load (internal/modules/cjs/loader.js:937:14)

Incorrectly converts string to number sometimes

Start out with this YAML file

timer: "1000"

Edit the JSON to change the timer value to "1001"

Expected result:

timer: "1001"

Actual result:

timer: 1001

As you can see, the string turned into an integer

I noticed sometimes it stays as a string depending on the structure of the document or the presence of comments but this is the simplest example that reproduces the issue I've been seeing. A pretty big one.

You can see it happening on the demo site too:
https://azimi.me/yawn-yaml/demo/index.html

Changing value in a multiple line value, thing breaks

Original YAML:

FOO: BAR
HELLO: WORLD

MULTIPLE_LINES_1: |
  Line 1
  Line 2
  Line 3
  Line 4

MULTIPLE_LINES_2: |
  Line 1
  Line 2
  Line 3
  Line 4

Modified JSON. Changing MULTIPLE_LINES_1 to:

{
  "FOO": "BAR",
  "HELLO": "WORLD",
  "MULTIPLE_LINES_1": "This line is changed",
  "MULTIPLE_LINES_2": "Line 1\nLine 2\nLine 3\nLine 4\n"
}

Output:

FOO: BAR
HELLO: WORLD

MULTIPLE_LINES_1: This line is changedMULTIPLE_LINES_2: |
  Line 1
  Line 2
  Line 3
  Line 4

Expected:

FOO: BAR
HELLO: WORLD

MULTIPLE_LINES_1: This line is changed

MULTIPLE_LINES_2: |
  Line 1
  Line 2
  Line 3
  Line 4

This is reproducible on http://azimi.me/yawn-yaml/demo/index.html, just paste Original YAML in first column and modified JSON in second column.

Window dependency

When I use this package in a node environment I am getting an error that says window undefined, which makes sense. Does this library only work in the browser?

This is the dist code and it accesses the dependencies through the window variable.

warning require.extensions is not supported by webpack. Use a loader instead.

I have the following warning with my create-react-app and typescript when I use yawn-yaml

Compiled with warnings.

./node_modules/yaml-js/lib/yaml.js
Critical dependency: require function is used in a way in which dependencies cannot be statically extracted

./node_modules/yaml-js/lib/yaml.js
require.extensions is not supported by webpack. Use a loader instead.

./node_modules/yaml-js/lib/yaml.js
require.extensions is not supported by webpack. Use a loader instead.

./node_modules/yaml-js/lib/yaml.js
require.extensions is not supported by webpack. Use a loader instead.

I have this error when I import yawn using

import Yawn from "yawn-yaml/cjs";

Changing a null value to a mapping results in `[object Object]` string value.

When changing a null value to an object, it gets sent to replacePrimitive, which turns it into the string [object Object].

yawn-yaml/src/index.js

Lines 224 to 232 in d870fbe

if (newValue !== value && !isArray(valNode.value)) {
// replace the value node
yaml = replacePrimitive(valNode, newValue, yaml);
// remove the key/value from newJson so it's not detected as new pair in
// later code
delete newJson[keyNode.value];
}

Cross-platform line break support

For this particular case, require('os').EOL should be used instead of the constant LINE_SEPERATOR, which has a value of \n.

yawn-yaml/src/index.js

Lines 317 to 320 in d870fbe

return yaml.substr(0, getNodeEndMark(node).pointer) +
LINE_SEPERATOR +
indentedValue +
yaml.substring(getNodeEndMark(node).pointer);

Flow styles are unsupported

For example, errors or invalid YAML result from loading and modifying the mapping or sequence in the following YAML document:

widgets: [flow, style]
gears: {flow: style}

Cannot load window properties

Hi
i'm trying to use your library with react and getting errors because window.yaml & window.jsyaml are not defined
tried to import it without success
when modify your code to load it as "require('js-yaml')" it worked
am i doing something wrong?

Thanks

Comments should remain associated with the element they comment

Executed:

const Yawn = require('yawn-yaml/cjs')
const input = `
- foo # 1st comment
- bar # 2nd comment
`
const original = new Yawn(input)
const json = original.json

json.push('biz')

original.json = json
console.log(original.yaml)

Expected:

- foo # 1st comment
- bar # 2nd comment
- biz

Observed:

- foo # 1st comment
- bar
- biz # 2nd comment

With:

    "yawn-yaml": {
      "version": "1.3.4",
      "resolved": "https://registry.npmjs.org/yawn-yaml/-/yawn-yaml-1.3.4.tgz",
      "integrity": "sha1-FMUaGUAkJJwi9TS20lQaVNCryt0=",
      "requires": {
        "js-yaml": "3.9.1",
        "lodash": "3.10.1",
        "yaml-js": "0.1.5"
      },
      "dependencies": {
        "lodash": {
          "version": "3.10.1",
          "resolved": "https://registry.npmjs.org/lodash/-/lodash-3.10.1.tgz",
          "integrity": "sha1-W/Rejkm6QYnhfUgnid/RW9FAt7Y="
        }
      }
    },

getRemark() does not function as expected

let str = `
# leading comment
value1: # inline comment
  sub: 1 # sub inline comment
# trailing comment`;

let yawn = new YAWN(str);

console.log("value1 remark:", yawn.getRemark('value1'));
console.log("value1.sub remark:", yawn.getRemark('value1.sub'));

yawn.setRemark('value1', "value1 new remark");
console.log("value1 remark:", yawn.getRemark('value1'));
console.log("value1.sub remark:", yawn.getRemark('value1.sub'));

Output:

value1 remark: sub inline comment
value1.sub remark: sub inline comment

value1 remark: value1 new remark
value1.sub remark: value1 new remark

The expected behavior would be that the returned value for value1's remark is inline comment, however, we get the remark of its child. This same issue means that any modifications to value1's remark modify its child, which you can see with the second set of print statements

Breaks when changing multiple values (due to non-deterministic behaviour)

Inside whatever non-deterministic magic is happening with setting a new object on yawn.json, if the whole object is replaced with more than one change, that cascades into causing miscellaneous parser errors to be thrown from yaml-js.

I only managed to find out why this was happening while trying to comment and uncomment huge swaths of my Yaml data files. I noticed when there was just one value changed, no errors were thrown, but if multiple values changed at the same time, yaml-js cried bloody murder with all sorts of ParserError.YAMLError errors, such as expected <block end>, but found <scalar> and special characters are not allowed

😢

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.