
Security News
Oxlint Introduces Type-Aware Linting Preview
Oxlint’s new preview brings type-aware linting powered by typescript-go, combining advanced TypeScript rules with native-speed performance.
serialise-js
Advanced tools
npm install serialise-js --save
Serialise any variable:
var serialise = require('serialise-js');
serialise({
foo: [1, 2, 3],
bar: { key: 'value' },
"the key": "the value"
});
Returns:
{
foo: [
1,
2,
3
],
bar: {
key: 'value'
},
'the key': 'the value'
}
You can pass in an options object as a second argument to serialise
:
indent
- Set the characters used for a single indent, default 2 spacesdoubleQuotes
- Set this to true
to serialise strings with double quotesinline
- Set this to true
to serialise object and arrays inline, set to a number to serialise inline below lines of this lengthExample:
var input = { foo: ['bar', { zim: 'gir' }] };
serialise(input, { indent: ' ', doubleQuotes: true, inline: 25 });
Returns:
{
foo: [
"bar",
{ zim: "gir" }
]
}
'[Circular]'
serialise.function
e.g. serialise.function = function() { return 'null'; }
It's easy to extend and modify the library.
Say you want to support serialisation of the popular moment.js library. By default moment will be serialised as a normal object, which is pretty gross. We can allow it to serialise as it would be defined (or close enough).
The first thing we need to do is create a serialisation method, these are all exposed on the serialise
function (e.g. serialise.string
). We can just add another one:
serialise.moment = function(moment, options) {
return 'moment(' +serialise.string(moment.format(), options) + ')';
};
This uses serialise.string
to serialise the formatted date string.
Next we need to create a type matching function to detect this type, and add it to serialise.types
:
serialise.types.moment = function(val) {
return val && val._isAMomentObject;
};
Finally we need to add this type to the typeOrder
array, which defines the order in this the types are checked, in our case we need it to be checked before object
, so we can just add it to the start of the array:
serialise.typeOrder.unshift('moment');
Now we can use it:
console.log(serialise({ time: moment('2015-01-01') }));
Which outputs:
{
time: moment('2015-01-01T00:00:00+00:00')
}
This could be neater, still, but you get the idea.
0.2.1
FAQs
An extensible library for serialising JavaScript data
The npm package serialise-js receives a total of 0 weekly downloads. As such, serialise-js popularity was classified as not popular.
We found that serialise-js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oxlint’s new preview brings type-aware linting powered by typescript-go, combining advanced TypeScript rules with native-speed performance.
Security News
A new site reviews software projects to reveal if they’re truly FOSS, making complex licensing and distribution models easy to understand.
Security News
Astral unveils pyx, a Python-native package registry in beta, designed to speed installs, enhance security, and integrate deeply with uv.