mirror of
https://github.com/flynx/diff.js.git
synced 2025-10-28 18:40:09 +00:00
rework...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
157761de03
commit
f6018a04f1
60
diff2.js
60
diff2.js
@ -204,14 +204,6 @@ module.OBJECT_LISTERS = {
|
|||||||
if(obj === null){
|
if(obj === null){
|
||||||
throw module.STOP } },
|
throw module.STOP } },
|
||||||
|
|
||||||
// XXX this is diff-specific...
|
|
||||||
text: function(obj){
|
|
||||||
return typeof(obj) == 'string'
|
|
||||||
&& obj.includes('\n')
|
|
||||||
&& obj.split(/\n/g).entries()
|
|
||||||
.map(function([k, v]){
|
|
||||||
return [[module.CONTENT, k], v] }) },
|
|
||||||
|
|
||||||
set: function(obj){
|
set: function(obj){
|
||||||
return obj instanceof Set
|
return obj instanceof Set
|
||||||
&& [...obj.values()]
|
&& [...obj.values()]
|
||||||
@ -253,28 +245,24 @@ module.OBJECT_LISTERS = {
|
|||||||
&& [['__proto__', obj.__proto__]] },
|
&& [['__proto__', obj.__proto__]] },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// XXX add function support...
|
// XXX add function support...
|
||||||
// XXX would be nice to be able to extend this...
|
// XXX this needs .name set correctly...
|
||||||
// e.g. make this a generic walker and for a diff-specific walker
|
|
||||||
// add text support...
|
|
||||||
var objectWalker =
|
var objectWalker =
|
||||||
module.objectWalker =
|
module.objectWalker =
|
||||||
Walk({
|
Walk({
|
||||||
//noText: true,
|
|
||||||
handler: function(obj, path, next, type){
|
handler: function(obj, path, next, type){
|
||||||
return type == 'LINK' ?
|
return type == 'LINK' ?
|
||||||
[path, 'LINK', next]
|
[path, 'LINK', next]
|
||||||
: [
|
: [
|
||||||
path,
|
path,
|
||||||
|
// null...
|
||||||
obj == null ?
|
obj == null ?
|
||||||
obj
|
obj
|
||||||
|
// objects...
|
||||||
: typeof(obj) == 'object' ?
|
: typeof(obj) == 'object' ?
|
||||||
{type: obj.constructor.name}
|
{type: obj.constructor.name}
|
||||||
// text...
|
// primitives...
|
||||||
: !this.noText
|
|
||||||
&& typeof(obj) == 'string'
|
|
||||||
&& obj.includes('\n') ?
|
|
||||||
{type: 'text'}
|
|
||||||
: obj,
|
: obj,
|
||||||
] },
|
] },
|
||||||
listers: module.OBJECT_LISTERS,
|
listers: module.OBJECT_LISTERS,
|
||||||
@ -288,6 +276,31 @@ Walk({
|
|||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
// like objectWalker(..) but with 'text' support...
|
||||||
|
//
|
||||||
|
var objectWalkerWithText =
|
||||||
|
module.objectWalkerWithText =
|
||||||
|
Object.assign(
|
||||||
|
object.create(objectWalker),
|
||||||
|
//object.create('objectWalkerWithText', objectWalker),
|
||||||
|
{
|
||||||
|
handler: function*(obj, path, next, type){
|
||||||
|
yield* !this.noText
|
||||||
|
&& typeof(obj) == 'string'
|
||||||
|
&& obj.includes('\n') ?
|
||||||
|
[ [path, {type: 'text'}] ]
|
||||||
|
: objectWalker.handler.call(this, ...arguments) },
|
||||||
|
listers: Object.assign(
|
||||||
|
{text: function(obj){
|
||||||
|
return typeof(obj) == 'string'
|
||||||
|
&& obj.includes('\n')
|
||||||
|
&& obj.split(/\n/g).entries()
|
||||||
|
.map(function([k, v]){
|
||||||
|
return [[module.CONTENT, k], v] }) }},
|
||||||
|
objectWalker.listers),
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
|
|
||||||
@ -856,6 +869,17 @@ console.log([
|
|||||||
text`)
|
text`)
|
||||||
.chain(serializePaths) ])
|
.chain(serializePaths) ])
|
||||||
|
|
||||||
|
console.log('---')
|
||||||
|
|
||||||
|
console.log([
|
||||||
|
...objectWalkerWithText(
|
||||||
|
`This
|
||||||
|
is
|
||||||
|
a
|
||||||
|
multiline
|
||||||
|
text`)
|
||||||
|
.chain(serializePaths) ])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**********************************************************************
|
/**********************************************************************
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user