mirror of
https://github.com/flynx/diff.js.git
synced 2025-10-29 02:50:10 +00:00
now text splitting works...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
2fcb8f9863
commit
5796055012
64
diff2.js
64
diff2.js
@ -426,7 +426,7 @@ module.WALK_HANDLERS = {
|
|||||||
attr: {
|
attr: {
|
||||||
walk: function(obj){
|
walk: function(obj){
|
||||||
return typeof(obj) == 'object'
|
return typeof(obj) == 'object'
|
||||||
&& [...Object.entries(obj)] } },
|
&& Object.entries(obj) } },
|
||||||
//*/
|
//*/
|
||||||
proto: {
|
proto: {
|
||||||
walk: function(obj){
|
walk: function(obj){
|
||||||
@ -434,11 +434,6 @@ module.WALK_HANDLERS = {
|
|||||||
&& obj.constructor.prototype !== obj.__proto__
|
&& obj.constructor.prototype !== obj.__proto__
|
||||||
&& [['__proto__', obj.__proto__]] }, },
|
&& [['__proto__', obj.__proto__]] }, },
|
||||||
|
|
||||||
text: {
|
|
||||||
walk: function(obj){
|
|
||||||
return typeof(obj) == 'string'
|
|
||||||
&& obj.includes('\n')
|
|
||||||
&& obj.split(/\n/g) } },
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
@ -446,6 +441,7 @@ module.WALK_HANDLERS = {
|
|||||||
// walk(<handler>, <path>[, <options>])
|
// walk(<handler>, <path>[, <options>])
|
||||||
// -> <walker>
|
// -> <walker>
|
||||||
//
|
//
|
||||||
|
//
|
||||||
// <handler>(<obj>, <path>, <next>, <type>)
|
// <handler>(<obj>, <path>, <next>, <type>)
|
||||||
// -> <value>
|
// -> <value>
|
||||||
// !> STOP(<value>)
|
// !> STOP(<value>)
|
||||||
@ -455,6 +451,7 @@ module.WALK_HANDLERS = {
|
|||||||
// -> <value>
|
// -> <value>
|
||||||
// !> STOP(<value>)
|
// !> STOP(<value>)
|
||||||
//
|
//
|
||||||
|
//
|
||||||
// <walker>(<obj>)
|
// <walker>(<obj>)
|
||||||
// <walker>(<obj>, <path>)
|
// <walker>(<obj>, <path>)
|
||||||
// -> <generator>
|
// -> <generator>
|
||||||
@ -468,7 +465,11 @@ module.WALK_HANDLERS = {
|
|||||||
var walk =
|
var walk =
|
||||||
module.walk =
|
module.walk =
|
||||||
function(handler, path=[], options={}){
|
function(handler, path=[], options={}){
|
||||||
var p = path
|
// normalize the handler...
|
||||||
|
var _handler =
|
||||||
|
handler instanceof types.Generator ?
|
||||||
|
handler
|
||||||
|
: function*(){ yield handler(...arguments) }
|
||||||
// parse args...
|
// parse args...
|
||||||
options =
|
options =
|
||||||
typeof(path) == 'object' && !(path instanceof Array) ?
|
typeof(path) == 'object' && !(path instanceof Array) ?
|
||||||
@ -481,12 +482,10 @@ function(handler, path=[], options={}){
|
|||||||
{ __proto__: module.HANDLE_DEFAULTS },
|
{ __proto__: module.HANDLE_DEFAULTS },
|
||||||
options)
|
options)
|
||||||
: options
|
: options
|
||||||
|
var handlers = options.handlers || module.WALK_HANDLERS
|
||||||
var _handler = function*(){
|
// XXX do we need this in options???
|
||||||
if(handler instanceof types.Generator){
|
var seen = options.seen = options.seen || new Map()
|
||||||
yield* handler(...arguments)
|
var p = path
|
||||||
} else {
|
|
||||||
yield handler(...arguments) } }
|
|
||||||
|
|
||||||
var _walk = function*(obj, path=p, type=undefined){
|
var _walk = function*(obj, path=p, type=undefined){
|
||||||
path = path instanceof Array ?
|
path = path instanceof Array ?
|
||||||
@ -496,22 +495,20 @@ function(handler, path=[], options={}){
|
|||||||
: []
|
: []
|
||||||
type = type || 'root'
|
type = type || 'root'
|
||||||
|
|
||||||
// handle loops...
|
// handle reference loops...
|
||||||
var seen = options.seen =
|
|
||||||
options.seen || new Map()
|
|
||||||
if(seen.has(obj)){
|
if(seen.has(obj)){
|
||||||
yield* _handler(obj, path, seen.get(obj), 'LINK')
|
yield* _handler(obj, path, seen.get(obj), 'LINK')
|
||||||
return }
|
return }
|
||||||
typeof(obj) == 'object'
|
typeof(obj) == 'object'
|
||||||
&& seen.set(obj, path)
|
&& seen.set(obj, path)
|
||||||
|
|
||||||
var handlers = options.handlers || module.WALK_HANDLERS
|
|
||||||
// format:
|
// format:
|
||||||
// [
|
// [
|
||||||
// [<handler-name>, [ [<key>, <value>], .. ]],
|
// [<handler-name>, [ [<key>, <value>], .. ]],
|
||||||
// ..
|
// ..
|
||||||
// ]
|
// ]
|
||||||
var next = Object.entries(handlers)
|
var next =
|
||||||
|
[...Object.entries(handlers)
|
||||||
// NOTE: we need this to support throwing STOP...
|
// NOTE: we need this to support throwing STOP...
|
||||||
.iter()
|
.iter()
|
||||||
.filter(function([n, h]){
|
.filter(function([n, h]){
|
||||||
@ -524,13 +521,14 @@ function(handler, path=[], options={}){
|
|||||||
return res
|
return res
|
||||||
&& [n, res] })
|
&& [n, res] })
|
||||||
.filter(function(e){
|
.filter(function(e){
|
||||||
return !!e })
|
return !!e }) ]
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// main object...
|
// main object...
|
||||||
yield* _handler(obj, path, next, type)
|
yield* _handler(obj, path, next, type)
|
||||||
// next/children...
|
// next/children...
|
||||||
yield* next
|
yield* next
|
||||||
|
.iter()
|
||||||
.map(function*([type, items]){
|
.map(function*([type, items]){
|
||||||
yield* items
|
yield* items
|
||||||
.iter()
|
.iter()
|
||||||
@ -1109,21 +1107,29 @@ console.log(JSON.stringify(diff(
|
|||||||
|
|
||||||
console.log('---')
|
console.log('---')
|
||||||
|
|
||||||
var walker = walk(function(e, p, n, t){
|
var walker = walk(function(obj, path, next, type){
|
||||||
return t == 'LINK' ?
|
// text...
|
||||||
[p, 'LINK', n]
|
if(typeof(obj) == 'string' && obj.includes('\n')){
|
||||||
|
next.push(['text', obj.split(/\n/g).entries()])
|
||||||
|
return [path, {type: 'text'}] }
|
||||||
|
|
||||||
|
return type == 'LINK' ?
|
||||||
|
[path, 'LINK', next]
|
||||||
|
//: type == 'text' ?
|
||||||
|
// [path, {type: 'text'}]
|
||||||
: [
|
: [
|
||||||
p,
|
path,
|
||||||
e == null ?
|
obj == null ?
|
||||||
e
|
obj
|
||||||
: typeof(e) == 'object' ?
|
: typeof(obj) == 'object' ?
|
||||||
{type: e.constructor.name}
|
{type: obj.constructor.name}
|
||||||
: e,
|
: obj,
|
||||||
] })
|
] })
|
||||||
|
|
||||||
// XXX test functions...
|
// XXX test functions...
|
||||||
console.log([
|
console.log([
|
||||||
...walker(o)
|
//...walker(o)
|
||||||
|
...walker(['this\nis\nsome\n\ttext'])
|
||||||
.chain(serializePaths) ])
|
.chain(serializePaths) ])
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user