Migrating - aMarCruz/jscc GitHub Wiki
jscc v1.0 is a complete rewrite. It is using TypeScript 3.0 and one enhanced set of test. It is more modularized and offers new characteristics, but has some breaking changes.
(TODO)
Support has been implemented for nested object properties.
This will output "Hello jscc":
//#set _OBJ = {p: {p2: {p3: 'Hello jscc'}}}
console.log('$_OBJ.p.p2.p3')
Additionally, the output of undefined
values has been optimized. In versions prior to v1, this code:
//#set _OBJ = { foo: 1 }
const foo = $_OBJ.bar
generated:
const foo = {"foo":1}.bar
In v1 the output makes more sense:
const foo = undefined
Unlike previous versions, the output of dates and regexes is the same that of the strings, so you can use them to regenerate the values.
For regexes, the replacement value is taken from the source
property and you don't need escape it, but it looses its flags.
For dates, the toJSON()
method is used.
Here some examples:
//#set _SBASE = /"[^"]"/.source
//#set _REGEX = RegExp(_SBASE + '|' + _SBASE.replace(/"/g, "'"))
const reForQuotedStrings = /$_REGEX/g
// output:
const reForQuotedStrings = /"[^"]"|'[^']'/g
//#set _DATE = new Date()
export const buildDate = new Date('$_DATE')
// output:
export const buildDate = new Date('2018-10-16T16:51:36.123Z')
The JSON output of those types is consistent with the above:
//#set _OBJ = {regex: _REGEX_, date: _DATE_}
const obj = $_OBJ
// output:
const obj = {"regex":"\"[^\"]\"|'[^']'","date":"2018-10-16T16:51:36.123Z"}
In JS, the output of NaN
has different behavior depending if it is wrapped on an object.
Since this is the correct behavior, things are confused when comparing or trying to convert to strings:
value | !!value | String(value) | JSON.stringify(value) |
---|---|---|---|
NaN |
false |
"NaN" | null |
new Number(NaN) |
true |
"NaN" | null |
new Date(NaN) |
true |
"Invalid Date" | null |
Infinity |
true |
"Infinity" | null |
-Infinity |
true |
"-Infinity" | null |
jscc v1 changes its behavior on this.
NaN
objects or values, alone, are always replaced by NaN
.
NaN
objects or values as JSON properties are replaced by null
.
Infinity is output "as is" if the target is the value alone.
In JSON output, Infinity
is replaced with Number.MAX_VALUE
and -Infinity
with Number.MIN_VALUE
, because the JSON specs does not allows non-finity numbers.
The result is in the following table:
$_VALUE | !!$_VALUE | $_VALUE | JSON output {p:_VALUE} |
---|---|---|---|
NaN |
false |
NaN |
{"p":"null"} |
new Number(NaN) |
false |
NaN |
{"p":"null"} |
new Date(NaN) |
false |
NaN |
{"p":"null"} |
Infinity |
true |
Infinity |
{"p":1.7976931348623157e+308} |
-Infinity |
true |
-Infinity |
{"p":5e-324} |