Skip to content

Commit d50db5e

Browse files
author
Saul van der Walt
committed
SuperModel works on node now
1 parent 209939d commit d50db5e

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

SuperModel.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
{ /* global Node NodeList HTMLInputElement HTMLTextAreaElement Text */
1+
{
22
const infinify = (fn, reflect = false) => new Proxy(fn, {
33
get: (fn, key) =>
4-
(reflect && key in fn && Reflect.get(fn, key)) || fn.bind(undefined, key)
4+
typeof key !== 'symbol' && ((reflect && key in fn && Reflect.get(fn, key)) || fn.bind(undefined, key))
55
})
66

77
const emitter = (host = {}, listeners = new Map()) => Object.assign(host, {
@@ -41,13 +41,21 @@
4141
})
4242
})
4343

44+
const NodeENV = typeof global === 'object'
45+
const root = NodeENV ? global : window
46+
const antiSemiotics = o => typeof o === 'symbol' ? o.toString() : o
47+
48+
if (NodeENV) {
49+
var Text = class Text { constructor (txt) { this.textContent = txt } }
50+
}
51+
4452
const isArr = Array.isArray
4553
const isDef = o => o !== undefined && o !== null
4654
const isNil = o => o === undefined || o === null
4755
const isObj = o => o && o.constructor === Object
4856
const isStr = o => typeof o === 'string'
49-
const isInput = o => o instanceof HTMLInputElement || o instanceof HTMLTextAreaElement
50-
const isArrlike = o => isArr(o) || o instanceof NodeList || (isDef(o) && !(o instanceof Function || o instanceof Node) && o.length % 1 === 0)
57+
const isInput = o => !NodeENV && (o instanceof root.HTMLInputElement || o instanceof root.HTMLTextAreaElement)
58+
const isArrlike = o => isArr(o) || (!NodeENV && o instanceof root.NodeList) || (isDef(o) && !(o instanceof Function || (!NodeENV && o instanceof root.Node)) && o.length % 1 === 0)
5159
const isPromise = o => typeof o === 'object' && isFunc(o.then)
5260
const isRegExp = o => o instanceof RegExp
5361
const isFunc = o => o instanceof Function
@@ -79,7 +87,7 @@
7987
}
8088

8189
// simple global variable but you could export here
82-
var SuperModel = (data, mitter = emitter(), store = new Map()) => {
90+
const SuperModel = (data, mitter = emitter(), store = new Map()) => {
8391
let Model
8492
const {emit, emitAsync, on, once} = mitter
8593

@@ -128,6 +136,8 @@
128136

129137
const syncs = new Map()
130138
const sync = new Proxy(function (obj, key, prop = key) {
139+
key = antiSemiotics(key)
140+
prop = antiSemiotics(prop)
131141
if (isArr(obj)) {
132142
const args = Array.from(arguments).slice(1)
133143
if (args.every(isStr)) return sync.template(obj, ...args)
@@ -183,9 +193,8 @@
183193
return obj
184194
}, {
185195
get (fn, prop) {
186-
if (Reflect.has(fn, prop)) {
187-
return Reflect.get(fn, prop)
188-
} else {
196+
if (Reflect.has(fn, prop)) return Reflect.get(fn, prop)
197+
else {
189198
return (obj, key) => {
190199
if (isNil(obj)) return sync.text(prop)
191200
if (isNil(key)) key = prop
@@ -211,7 +220,7 @@
211220

212221
sync.text = new Proxy(
213222
prop => sync(new Text(), 'textContent', prop),
214-
{get: (fn, prop) => fn(prop)}
223+
{get: (fn, prop) => fn(antiSemiotics(prop))}
215224
)
216225

217226
sync.template = (strings, ...keys) => flatten(
@@ -225,13 +234,15 @@
225234

226235
const Async = new Proxy((key, fn) => has(key) ? fn(store.get(key)) : once('set:' + key, fn), {
227236
get: (_, key) => new Promise(resolve => {
237+
key = antiSemiotics(key)
228238
has(key) ? resolve(store.get(key)) : once('set:' + key, resolve)
229239
}),
230240
set: (_, key, val) => val.then(mut.bind(undefined, key))
231241
})
232242

233243
const validators = new Map()
234244
const validateProp = key => {
245+
key = antiSemiotics(key)
235246
const valid = store.has(key) && validators.has(key) && validators.get(key)(store.get(key))
236247
emit('validate:' + key, valid)
237248
emit('validate', key, valid)
@@ -240,6 +251,7 @@
240251

241252
const Validation = new Proxy((key, validator) => {
242253
if (isNil(validator)) return validateProp(key)
254+
key = antiSemiotics(key)
243255
if (isRegExp(validator)) {
244256
const regexp = validator
245257
validator = val => isStr(val) && regexp.test(val)
@@ -252,11 +264,12 @@
252264
}
253265
}, {
254266
get: (_, key) => validateProp(key),
255-
set: (vd, key, val) => vd(key, val)
267+
set: (validate, key, val) => validate(key, val)
256268
})
257269

258270
const computed = new Map()
259271
const compute = new Proxy(function (key, computation) {
272+
key = antiSemiotics(key)
260273
if (isFunc(computation)) computed.set(key, computation)
261274
else if (isStr(computation)) {
262275
if (allare(arguments, isStr)) {
@@ -345,4 +358,6 @@
345358
return Model
346359
}
347360
SuperModel.emitter = emitter
361+
362+
isFunc(root.define) && root.define.amd ? root.define([], () => SuperModel) : typeof module === 'object' && module.exports ? module.exports = SuperModel : root.SuperModel = SuperModel
348363
}

SuperModel.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)