-
-
Notifications
You must be signed in to change notification settings - Fork 770
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Export built-in types, type override now preserves order
- Loading branch information
Showing
9 changed files
with
196 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ env: | |
es6: true | ||
|
||
parserOptions: | ||
ecmaVersion: 2017 | ||
ecmaVersion: 2020 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// This example overrides built-in !!int type to return BigInt instead of a Number | ||
// | ||
|
||
'use strict'; | ||
|
||
/*global BigInt*/ | ||
/*eslint-disable no-console*/ | ||
|
||
const util = require('util'); | ||
const yaml = require('../'); | ||
|
||
|
||
// keep most of the original `int` options as is | ||
let options = Object.assign({}, yaml.types.int.options); | ||
|
||
options.construct = data => { | ||
let value = data, sign = 1n, ch; | ||
|
||
if (value.indexOf('_') !== -1) { | ||
value = value.replace(/_/g, ''); | ||
} | ||
|
||
ch = value[0]; | ||
|
||
if (ch === '-' || ch === '+') { | ||
if (ch === '-') sign = -1n; | ||
value = value.slice(1); | ||
ch = value[0]; | ||
} | ||
|
||
return sign * BigInt(value); | ||
}; | ||
|
||
|
||
options.predicate = object => { | ||
return Object.prototype.toString.call(object) === '[object BigInt]' || | ||
yaml.types.int.options.predicate(object); | ||
}; | ||
|
||
|
||
let BigIntType = new yaml.Type('tag:yaml.org,2002:int', options); | ||
|
||
const SCHEMA = yaml.DEFAULT_SCHEMA.extend({ implicit: [ BigIntType ] }); | ||
|
||
const data = ` | ||
bigint: -12_345_678_901_234_567_890 | ||
`; | ||
|
||
const loaded = yaml.load(data, { schema: SCHEMA }); | ||
|
||
console.log('Parsed as:'); | ||
console.log('-'.repeat(70)); | ||
console.log(util.inspect(loaded, false, 20, true)); | ||
|
||
console.log(''); | ||
console.log(''); | ||
console.log('Dumped as:'); | ||
console.log('-'.repeat(70)); | ||
console.log(yaml.dump(loaded, { schema: SCHEMA })); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ env: | |
es6: true | ||
|
||
parserOptions: | ||
ecmaVersion: 2017 | ||
ecmaVersion: 2020 | ||
|
||
rules: | ||
no-undefined: 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'use strict'; | ||
|
||
/* eslint-disable no-use-before-define, new-cap */ | ||
|
||
|
||
const assert = require('assert'); | ||
const yaml = require('../../'); | ||
|
||
|
||
it('Should allow custom formatting through implicit custom tags', function () { | ||
function CustomDump(data, opts) { | ||
if (!(this instanceof CustomDump)) return new CustomDump(data, opts); | ||
this.data = data; | ||
this.opts = opts; | ||
} | ||
|
||
CustomDump.prototype.represent = function () { | ||
let result = yaml.dump(this.data, Object.assign({ replacer, schema }, this.opts)); | ||
result = result.trim(); | ||
if (result.includes('\n')) result = '\n' + result; | ||
return result; | ||
}; | ||
|
||
|
||
let CustomDumpType = new yaml.Type('!format', { | ||
kind: 'scalar', | ||
resolve: () => false, | ||
instanceOf: CustomDump, | ||
represent: d => d.represent() | ||
}); | ||
|
||
|
||
let schema = yaml.DEFAULT_SCHEMA.extend({ implicit: [ CustomDumpType ] }); | ||
|
||
function replacer(key, value) { | ||
if (key === '') return value; // top-level, don't change this | ||
if (key === 'flow_choices') return CustomDump(value, { flowLevel: 0 }); | ||
if (key === 'block_choices') return CustomDump(value, { flowLevel: Infinity }); | ||
return value; // default | ||
} | ||
|
||
let result = CustomDump({ flow_choices : [ 1, 2 ], block_choices: [ 4, 5 ] }).represent().trim(); | ||
|
||
assert.strictEqual(result, ` | ||
flow_choices: [1, 2] | ||
block_choices: | ||
- 4 | ||
- 5`.replace(/^\n/, '')); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict'; | ||
|
||
/* global BigInt */ | ||
|
||
|
||
const assert = require('assert'); | ||
const yaml = require('../../'); | ||
|
||
|
||
it('Should allow int override', function () { | ||
let options = Object.assign({}, yaml.types.int.options); | ||
|
||
options.construct = data => { | ||
let value = data, sign = 1n, ch; | ||
|
||
if (value.indexOf('_') !== -1) { | ||
value = value.replace(/_/g, ''); | ||
} | ||
|
||
ch = value[0]; | ||
|
||
if (ch === '-' || ch === '+') { | ||
if (ch === '-') sign = -1n; | ||
value = value.slice(1); | ||
ch = value[0]; | ||
} | ||
|
||
return sign * BigInt(value); | ||
}; | ||
|
||
|
||
let BigIntType = new yaml.Type('tag:yaml.org,2002:int', options); | ||
|
||
const SCHEMA = yaml.DEFAULT_SCHEMA.extend({ implicit: [ BigIntType ] }); | ||
|
||
const data = ` | ||
int: -123_456_789 | ||
bigint: -12_345_678_901_234_567_890 | ||
float: -12_345_678_901_234_567_890.1234 | ||
`; | ||
|
||
assert.deepStrictEqual(yaml.load(data, { schema: SCHEMA }), { | ||
int: -123456789n, | ||
bigint: -12345678901234567890n, | ||
float: -12345678901234567000 // precision loss expected | ||
}); | ||
}); |