Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Date and Set #15

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions demo/public/iframe.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@

const longMap = new Map(Object.entries(longObj))
console.log(longMap)
console.log('Date', new Date())
console.log(
'Map',
new Map([
[1, 2],
[3, 4],
])
)
console.log('Set', new Set([1, 2, 3]))
console.timeEnd('Render time')
console.dir(new Error('hmm'))

Expand Down
38 changes: 38 additions & 0 deletions src/Transform/Date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
interface Storage {
name: string
body: object
proto: string
}

/**
* Serialize a Date into JSON
*/
export default {
type: 'Date',
lookup: Date,
shouldTransform(type: any, obj: any) {
return obj && obj.constructor && obj.constructor.name === 'Date'
},
toSerializable(date: any): Storage {
let body = { date: date.toString() }

return {
name: 'Date',
body,
proto: Object.getPrototypeOf(date).constructor.name,
}
},
fromSerializable(data: Storage) {
const { body } = data
let obj = { ...body }

if (typeof data.proto === 'string') {
// @ts-ignore
obj.constructor = {
name: data.proto,
}
}

return obj
},
}
54 changes: 54 additions & 0 deletions src/Transform/Set.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
interface Storage {
name: string
body: any[]
proto: string
}

/**
* Serialize a Set into JSON
*/
const REMAINING_KEY = '__console_feed_remaining__'
export default {
type: 'Set',
lookup: Set,
shouldTransform(type: any, obj: any) {
return obj && obj.constructor && obj.constructor.name === 'Set'
},
toSerializable(set: any): Storage {
let body = []

set.forEach(function (value, key) {
const v = typeof value == 'object' ? JSON.stringify(value) : value
if (typeof v === 'string' && v.includes(REMAINING_KEY)) {
return
}
body.push(value)
})

return {
name: 'Set',
body,
proto: Object.getPrototypeOf(set).constructor.name,
}
},
fromSerializable(data: Storage) {
const { body } = data
let obj = {
...body.filter((value) => {
if (typeof value !== 'string') return true
return !value.includes(REMAINING_KEY)
}),
}

obj = obj

if (typeof data.proto === 'string') {
// @ts-ignore
obj.constructor = {
name: data.proto,
}
}

return obj
},
}
4 changes: 3 additions & 1 deletion src/Transform/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Message } from '../definitions/Console'
import Arithmetic from './arithmetic'
import BigInt from './BigInt'
import Date from './Date'
import Function from './Function'
import HTML from './HTML'
import Map from './Map'

import Replicator from './replicator'
import Set from './Set'

const transforms = [HTML, Function, Arithmetic, Map, BigInt]
const transforms = [HTML, Function, Arithmetic, Map, BigInt, Date, Set]

const replicator = new Replicator()
replicator.addTransforms(transforms)
Expand Down
Loading