0.3.0 • Published 7 years ago

yamljs v0.3.0

Weekly downloads
1,574,560
License
MIT
Repository
github
Last release
7 years ago

yaml.js

Build status

Standalone JavaScript YAML 1.2 Parser & Encoder. Works under node.js and all major browsers. Also brings command line YAML/JSON conversion tools.

Mainly inspired from Symfony Yaml Component.

How to use

Import yaml.js in your html page:

<script type="text/javascript" src="yaml.js"></script>

Parse yaml string:

nativeObject = YAML.parse(yamlString);

Dump native object into yaml string:

yamlString = YAML.stringify(nativeObject[, inline /* @integer depth to start using inline notation at */[, spaces /* @integer number of spaces to use for indentation */] ]);

Load yaml file:

nativeObject = YAML.load('file.yml');

Load yaml file:

YAML.load('file.yml', function(result)
{
    nativeObject = result;
});

Use with node.js

Install module:

npm install yamljs

Use it:

YAML = require('yamljs');

// parse YAML string
nativeObject = YAML.parse(yamlString);

// Generate YAML
yamlString = YAML.stringify(nativeObject, 4);

// Load yaml file using YAML.load
nativeObject = YAML.load('myfile.yml');

Command line tools

You can enable the command line tools by installing yamljs as a global module:

npm install -g yamljs

Then, two cli commands should become available: yaml2json and json2yaml. They let you convert YAML to JSON and JSON to YAML very easily.

yaml2json

usage: yaml2json [-h] [-v] [-p] [-i INDENTATION] [-s] [-r] [-w] input

Positional arguments:
  input                 YAML file or directory containing YAML files.

Optional arguments:
  -h, --help            Show this help message and exit.
  -v, --version         Show program's version number and exit.
  -p, --pretty          Output pretty (indented) JSON.
  -i INDENTATION, --indentation INDENTATION
                        Number of space characters used to indent code (use 
                        with --pretty, default: 2).
  -s, --save            Save output inside JSON file(s) with the same name.
  -r, --recursive       If the input is a directory, also find YAML files in 
                        sub-directories recursively.
  -w, --watch           Watch for changes.

json2yaml

usage: json2yaml [-h] [-v] [-d DEPTH] [-i INDENTATION] [-s] [-r] [-w] input

Positional arguments:
  input                 JSON file or directory containing JSON files.

Optional arguments:
  -h, --help            Show this help message and exit.
  -v, --version         Show program's version number and exit.
  -d DEPTH, --depth DEPTH
                        Set minimum level of depth before generating inline 
                        YAML (default: 2).
  -i INDENTATION, --indentation INDENTATION
                        Number of space characters used to indent code 
                        (default: 2).
  -s, --save            Save output inside YML file(s) with the same name.
  -r, --recursive       If the input is a directory, also find JSON files in 
                        sub-directories recursively.
  -w, --watch           Watch for changes.

examples

# Convert YAML to JSON and output resulting JSON on the console
yaml2json myfile.yml

# Store output inside a JSON file
yaml2json myfile.yml > output.json

# Output "pretty" (indented) JSON
yaml2json myfile.yml --pretty

# Save the output inside a file called myfile.json
yaml2json myfile.yml --pretty --save

# Watch a full directory and convert any YAML file into its JSON equivalent
yaml2json mydirectory --pretty --save --recursive

# Convert JSON to YAML and store output inside a JSON file
json2yaml myfile.json > output.yml

# Output YAML that will be inlined only after 8 levels of indentation
json2yaml myfile.json --depth 8

# Save the output inside a file called myfile.json with 4 spaces for each indentation
json2yaml myfile.json --indentation 4

# Watch a full directory and convert any JSON file into its YAML equivalent
json2yaml mydirectory --pretty --save --recursive
@nudodesigns/noxy-epos-types@nudodesigns/noxy-loyalty-types@nudodesigns/types@things-factory/operato-hub@sooha/uikit@mathigon/parser@platformer/platformer-clihoney-lending-eligibilityhoney-lending-platform-adapterhoney-lending-transactionsspfa-theme-defaultlocal-build-preprocessor@spokedev/zolaapi-parceirosentry2libmxdocker@manwaring/serverless-test-helper@vangware/jsepapi-gateway-coreapi-gateway-pack@phparkle/semantic-ui@ibanfi/angus-clipscn-commonlib-tsspfa-yamleslint-plugin-swaggerangusy-cli@enbock/time-tracker@nchannel/endpoint-sdkdocsogmojaloop-fxp-scheme-adapterdiva-backend-commons@mygooder/semanticcoco-slackopenapi-mongoose@viewdo/dxp-urls-clityoagexecutable_esm_aa_webcomponentsminmod.tspp-assistorl7mpsumrex-client@dimensionfourcloud/consul@dimensionfourcloud/rbac@dimensionfourcloud/boot@dimensionfourcloud/config@dimensionfourcloud/service@modngn/cli@startrco/slate-toolsloymax-armsmart-api-booking-service@containership/containership.schema-convertercs-openrole-frameworkpeopledata-cli@map-colonies/external-to-osm-tag-mapping@lucas-boaideas/test-toolsts-vue2-cosen@smoothjs/configkaena-tsemgen-auth-proxy@ued_fpi/fpi-web-cli@roman-hotsiy/redocly-dev-portal@project-furnace/furnace-cli@githaxs/config-generator@dotedu/ivx-cli-urls@spokedev/fab_utilsMirroracyort-configacyort-helperacyort-render@winwin/hexo-editor-serverdr-comp-packagemomenta-backendcol-servercycle-utils@mathigon/textbooks-parserxh-utilserver-decorsletsdraptor-frameworkrmr-specsrmr-toolsbannerbot-servicetuscanaalekhgnextia-serverlessnetworkxeyezondexfreightexpress-objection-starterlynx-appquietly@amosah/resumefomantic-ui-semeiaouterstackcsupmio-mock@teamfabric/create-fabric-connectorproject.yamlwholesale-forwardingapi-test-yaml
0.3.0

7 years ago

0.2.10

7 years ago

0.2.9

7 years ago

0.2.8

8 years ago

0.2.7

8 years ago

0.2.6

8 years ago

0.2.5

8 years ago

0.2.4

9 years ago

0.2.3

9 years ago

0.2.2

9 years ago

0.2.1

9 years ago

0.2.0

10 years ago

0.1.6

10 years ago

0.1.5

10 years ago

0.1.4

11 years ago

0.1.3

12 years ago

0.1.2

12 years ago

0.1.1

12 years ago

0.1.0

12 years ago