skeyma
skeyma is a JavaScript parser & serializer for {key, value}
objects & streams. It's useful for storing and retreiving levelup object streams.
Examples
Using skeyma for objects
const template = "${forumId}/${postId}/${commentId}"const parse serialize = const object = text: "Hello." forumId: "1" postId: "2" commentId: "3" console// { text: 'Hello.', forumId: '1', postId: '2', commentId: '3' } console// { key: '1/2/3', value: { text: 'Hello.' } } console// { text: 'Hello.', forumId: '1', postId: '2', commentId: '3' }
Using skeyma for object streams
const template = "${forumId}/${postId}/${commentId}"const parse serialize = const object = text: "Hello." forumId: "1" postId: "2" commentId: "3"const objects = objectsobjects objects // { text: 'Hello.', forumId: '1', postId: '2', commentId: '3' }// { key: '1/2/3', value: { text: 'Hello.' } }// { text: 'Hello.', forumId: '1', postId: '2', commentId: '3' }
API
let {parse, serialize} = skeyma(template)
Returns a parser/serializer instance based on the template
string, in which object keys are delimited by ${...}
, similar to ES6 template strings.
let kv = serialize([obj])
Returns a new {key, value}
object from the original object. The key
is rendered from the schema template, and the value is a copy of the original object, with the key fields removed. If no object is provided, a serialize transform is returned.
let obj = parse([kv])
Returns a new object from the original {key, value}
. The object is a copy of the original value
, with additional fields extracted from the key
according to the template. If no object is provided, a parse transform is returned.