Comparing version 6.1.1 to 6.3.0
105
lib/main.js
@@ -331,46 +331,52 @@ (function() { | ||
//--------------------------------------------------------------------------------------------------------- | ||
cram(name, ...content) { | ||
var $key, attributes, d1, d2, has_attributes, i, idx, len, part, template; | ||
if ((isa.text(name)) && (content.length === 0)) { | ||
return super.cram(this.settings.DATOM.new_single_datom(name)); | ||
_analyze(name, tail) { | ||
/* NOTE to be overriden by derivatives as seen necessary */ | ||
var attributes, content, i, idx, len, part, type; | ||
attributes = []; | ||
content = []; | ||
for (idx = i = 0, len = tail.length; i < len; idx = ++i) { | ||
part = tail[idx]; | ||
switch (type = type_of(part)) { | ||
case 'object': | ||
attributes.push(part); | ||
break; | ||
case 'function': | ||
content.push(part); | ||
break; | ||
case /* NOTE always leave as-is, expanded by Cupofjoe */'text': | ||
content.push(this.settings.DATOM.new_single_datom('text', { | ||
text: part | ||
})); | ||
break; | ||
default: | ||
content.push(this.settings.DATOM.new_single_datom('value', { | ||
$value: part | ||
})); | ||
} | ||
} | ||
return {name, attributes, content}; | ||
} | ||
//--------------------------------------------------------------------------------------------------------- | ||
cram(name, ...tail) { | ||
var attributes, content, d1, has_attributes, v; | ||
// XXX_SUPER = @Cupofjoe. | ||
({name, attributes, content} = this._analyze(name, tail)); | ||
has_attributes = false; | ||
template = null; | ||
if (this.settings.absorb) { | ||
if (isa.object(name)) { | ||
[name, template] = [null, name]; | ||
} | ||
attributes = {}; | ||
for (idx = i = 0, len = content.length; i < len; idx = ++i) { | ||
part = content[idx]; | ||
if (!isa.object(part)) { | ||
continue; | ||
} | ||
has_attributes = true; | ||
Object.assign(attributes, part); | ||
content.splice(idx, 1); | ||
} | ||
//....................................................................................................... | ||
if ((attributes != null) && attributes.length > 0) { | ||
has_attributes = true; | ||
attributes = Object.assign({}, ...attributes); | ||
} | ||
if (name === null) { | ||
if ((!has_attributes) && (template == null)) { | ||
//....................................................................................................... | ||
if (has_attributes && name === null) { | ||
v = rpr({name, attributes, content}); | ||
throw new Error(`^datom/cupofjoe@3498^ cannot have attributes without name, got ${v}`); | ||
} | ||
//....................................................................................................... | ||
if ((content != null) && content.length > 0) { | ||
if (name === null) { | ||
return super.cram(...content); | ||
} | ||
if (template != null) { | ||
if (content.length > 0) { | ||
throw new Error(`^datom/cupofjoe@3412^ cannot have template and content, got ${rpr({template, content})}`); | ||
} | ||
template = Object.assign({}, template, attributes); | ||
if (($key = template.$key) == null) { | ||
throw new Error(`^datom/cupofjoe@3442^ cannot have template without $key, got ${rpr({template})}`); | ||
} | ||
delete template.$key; | ||
return super.cram(this.settings.DATOM.new_datom($key, template)); | ||
} | ||
} else if (content.length === 0) { | ||
if (has_attributes) { | ||
return super.cram(this.settings.DATOM.new_single_datom(name, attributes)); | ||
} | ||
return super.cram(this.settings.DATOM.new_single_datom(name)); | ||
} else { | ||
if (has_attributes) { | ||
d1 = this.settings.DATOM.new_open_datom(name, attributes); | ||
@@ -380,19 +386,12 @@ } else { | ||
} | ||
return super.cram(d1, ...content, this.settings.DATOM.new_close_datom(name)); | ||
} | ||
d2 = this.settings.DATOM.new_close_datom(name); | ||
return super.cram(d1, ...content, d2); | ||
} | ||
//--------------------------------------------------------------------------------------------------------- | ||
expand() { | ||
var R, i, idx, len, text; | ||
R = super.expand(); | ||
for (idx = i = 0, len = R.length; i < len; idx = ++i) { | ||
text = R[idx]; | ||
if (!isa.text(text)) { | ||
continue; | ||
} | ||
R[idx] = this.settings.DATOM.new_datom('^text', {text}); | ||
//....................................................................................................... | ||
if (has_attributes) { | ||
return super.cram(this.settings.DATOM.new_single_datom(name, attributes)); | ||
} | ||
return R; | ||
if (name !== null) { | ||
return super.cram(this.settings.DATOM.new_single_datom(name)); | ||
} | ||
return null; | ||
} | ||
@@ -399,0 +398,0 @@ |
{ | ||
"name": "datom", | ||
"version": "6.1.1", | ||
"version": "6.3.0", | ||
"description": "standardized immutable objects in the spirit of datomic, especially suited for use in data pipelines", | ||
@@ -5,0 +5,0 @@ "main": "lib/main.js", |
@@ -471,3 +471,42 @@ | ||
Call patterns: | ||
* first argument is always: | ||
* the **basic name** (the `$key` of the datom minus the sigil) of the datom, | ||
* or else the **extended name**, where implemented (for example in InterText `CupOfHtml`, this means one | ||
can give `div#c59.draggable.hilite` as first argument to produce elements with a tag name (`div`), an ID | ||
(`c59`), and HTML `class` attribute (`draggable hilite`) in one go | ||
* or else **`null`** to indicate absence of a specific name | ||
* when a name has been given | ||
* and there are content arguments, then a pair of `{ $key: '<name', }`, `{ $key: '>name', }` datoms | ||
will be produced, with the content arguments coming in between | ||
* in case no content has been given, a single `{ $key: '^name', }` datom will be produced | ||
* of second and following arguments, | ||
* if the instance has been set to `{ absorb: true, }` and the second argument is an object, then the | ||
second argument—and only the second one—is treated as an attributes arguments to the datom that is to be | ||
produced (similar to how `DATOM.new_datom()` works), so `cram 'foo', { id: 'c221', frob: true, }` will | ||
produce `{ $key: '^foo', id: 'c221', frob: true, }`. | ||
* In case a key/value pair attributes argument conflicts with one set by an extended name (as in `cram | ||
'foo#IDA', { id: 'IDB', }`), the one in the attributes argument wins (as it would in a similar | ||
situation when using `Object.assign()`) | ||
* in case a content argument is a function, that function will be called without arguments. | ||
* if the function itself calls `cram()` from the same instance, its return value will be discarded; | ||
* in case it does not call `cram()`, its return value will be discarded if it is `null` or `undefined`, | ||
and otherwise become a content argument. | ||
* in the base implemention, content arguments produce a series of 'value datoms'; e.g. `cram null, 42, | ||
'some text', true` will emit `{ $key: '^value', $value: 42, }, { $key: '^value', $value: 'some text', }, | ||
{ $key: '^value', $value: true, }` | ||
* derivatives may emit other datoms for calls with `null`, e.g. `CupOfHtml` will produce `^text` datoms | ||
```coffee | ||
cram name | ||
cram name, content1, content2, ... | ||
cram name, content1, ( -> function ), ... | ||
cram name, content1, ( -> cram ... ), ... | ||
cram name, { key: value, }, content1, ( -> cram ... ), ... | ||
``` | ||
> **TAINT** should content inserted via return value be subject to same process as `cram()`med content? | ||
# To Do | ||
@@ -474,0 +513,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
161222
542
883