Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@jaysalvat/smart-model

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jaysalvat/smart-model - npm Package Compare versions

Comparing version 0.2.15 to 0.3.0

.vscode/settings.json

2

build/smart-model.esm.js

@@ -5,3 +5,3 @@ /**!

* https://github.com/jaysalvat/smart-model
* @version 0.2.15 built 2021-02-22 07:45:48
* @version 0.3.0 built 2021-02-22 09:17:46
* @license ISC

@@ -8,0 +8,0 @@ * @author Jay Salvat http://jaysalvat.com

@@ -1,2 +0,2 @@

/*! SmartModel v0.2.15 */
/*! SmartModel v0.3.0 */
class e extends Error{constructor(e){super(e.message),Object.assign(this,e)}}function t(e){return Array.isArray(e)}function r(e){return void 0===e}function n(e){return""===e||null===e||r(e)}function o(e){return"function"==typeof e}function s(e){return e.toString().startsWith("class")}function c(e){return[].concat([],e)}function u(e,r,o){const u=[];return e.required&&n(o)?(u.push({message:`Invalid value 'required' on property '${r}'`,code:"required"}),u):(void 0===o||(!e.type||!e.required&&n(o)||c(e.type).some((e=>function(e,r){const n=r&&r.toString().match(/^\s*function (\w+)/),o=(n?n[1]:"object").toLowerCase();if("date"===o&&e instanceof r)return!0;if("array"===o&&t(e))return!0;if("object"===o){if(s(r)&&e instanceof r)return!0;if(!s(r)&&typeof e===o)return!0}else if(typeof e===o)return!0;return!1}(o,e)))||u.push({message:`Invalid type '${typeof o}' on property '${r}'`,code:"type"}),e.rule&&Object.keys(e.rule).forEach((t=>{(0,e.rule[t])(o)&&u.push({message:`Invalid value '${t}' on property '${r}'`,code:t})}))),u)}function i(e={},t,r){if(!e.type)return!1;const n=e.type.prototype instanceof a&&e.type,o="[object Object]"===e.type.toString()&&e.type;return!(!n&&!o)&&(n||a.create(t.normalize("NFD").replace(/[\u0300-\u036f]/g,"").match(/[a-z]+/gi).map((e=>e.charAt(0).toUpperCase()+e.substr(1).toLowerCase())).join(""),o,r))}class a extends class{constructor(t,r){return new Proxy(this,{set(n,s,c){let a=t[s];const f=n[s],p=(l=f,!(JSON.stringify(c)===JSON.stringify(l)));var l;const d=i(a,s,r);function y(e,r){return Reflect.apply(e,n,r||[s,c,f,t])}if(!a){if(r.strict)return!0;a={}}if(y(n.onBeforeSet),p&&y(n.onBeforeUpdate),o(a.transform)&&(c=y(a.transform,[c,t])),r.exceptions){const t=u(a,s,c);if(t.length)throw new e({message:t[0].message,property:s,code:t[0].code,source:n.constructor.name})}return d&&(c=new d(c instanceof Object?c:{})),n[s]=c,y(n.onSet),p&&y(n.onUpdate),!0},get(e,r){const n=t[r];let s=e[r];if(!n)return e[r];function c(n,o){return Reflect.apply(n,e,o||[r,s,t])}return c(e.onBeforeGet),o(n)&&(s=c(n,[e,t])),o(n.format)&&(s=c(n.format,[s,t])),c(e.onGet),s},deleteProperty(r,n){const o=r[n];function s(e,s){return Reflect.apply(e,r,s||[n,o,t])}if(t[n].required)throw new e({message:`Invalid delete on required propery ${n}`,property:n,code:"required"});return s(r.onBeforeDelete),Reflect.deleteProperty(r,n),s(r.onDelete),s(r.onUpdate),!0}})}}{constructor(e={},t={},n){super(e,n),Object.keys(e).forEach((n=>{r(t[n])&&(this[n]=r(e[n].default)?t[n]:e[n].default)})),this.feed(t)}feed(e){Object.keys(e).forEach((t=>{this[t]=e[t]}))}onBeforeGet(){}onBeforeSet(){}onBeforeUpdate(){}onDelete(){}onGet(){}onBeforeDelete(){}onSet(){}onUpdate(){}}a.settings={strict:!1,exceptions:!0},a.create=function(e,r,n,o){n=Object.assign({},a.settings,n);const s={[e]:class extends a{constructor(e){super(r,e,n)}}}[e];return s.checkErrors=function(e,t){const o={};return Object.keys(r).forEach((s=>{let a;const f=e[s],p=r[s],l=i(p,s,n);l&&(a=l.checkErrors(f,t));let d=u(p,s,f);a?o[s]=a:d.length&&(t&&(d=d.filter((e=>!c(t).includes(e.code)))),d.length&&(o[s]=d))})),!!Object.keys(o).length&&o},s.hydrate=function(e){return t(e)?e.map((e=>new s(e))):new s(e)},Object.assign(s.prototype,o),s.schema=r,s};export default a;

@@ -5,3 +5,3 @@ /**!

* https://github.com/jaysalvat/smart-model
* @version 0.2.15 built 2021-02-22 07:45:48
* @version 0.3.0 built 2021-02-22 09:17:46
* @license ISC

@@ -11,5 +11,5 @@ * @author Jay Salvat http://jaysalvat.com

(function(global, factory) {
typeof exports === "object" && typeof module !== "undefined" ? factory(exports) : typeof define === "function" && define.amd ? define([ "exports" ], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self,
factory(global.SmartModel = {}));
})(this, (function(exports) {
typeof exports === "object" && typeof module !== "undefined" ? module.exports = factory() : typeof define === "function" && define.amd ? define(factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self,
global.SmartModel = factory());
})(this, (function() {
"use strict";

@@ -276,6 +276,3 @@ class SmartModelError extends Error {

};
exports.default = SmartModel;
Object.defineProperty(exports, "__esModule", {
value: true
});
return SmartModel;
}));

@@ -1,2 +0,2 @@

/*! SmartModel v0.2.15 */
var e,t;e=this,t=function(e){"use strict";class t extends Error{constructor(e){super(e.message),Object.assign(this,e)}}function r(e){return Array.isArray(e)}function n(e){return void 0===e}function o(e){return""===e||null===e||n(e)}function s(e){return"function"==typeof e}function c(e){return e.toString().startsWith("class")}function i(e){return[].concat([],e)}function u(e,t,n){const s=[];return e.required&&o(n)?(s.push({message:`Invalid value 'required' on property '${t}'`,code:"required"}),s):(void 0===n||(!e.type||!e.required&&o(n)||i(e.type).some((e=>function(e,t){const n=t&&t.toString().match(/^\s*function (\w+)/),o=(n?n[1]:"object").toLowerCase();if("date"===o&&e instanceof t)return!0;if("array"===o&&r(e))return!0;if("object"===o){if(c(t)&&e instanceof t)return!0;if(!c(t)&&typeof e===o)return!0}else if(typeof e===o)return!0;return!1}(n,e)))||s.push({message:`Invalid type '${typeof n}' on property '${t}'`,code:"type"}),e.rule&&Object.keys(e.rule).forEach((r=>{(0,e.rule[r])(n)&&s.push({message:`Invalid value '${r}' on property '${t}'`,code:r})}))),s)}function f(e={},t,r){if(!e.type)return!1;const n=e.type.prototype instanceof a&&e.type,o="[object Object]"===e.type.toString()&&e.type;return!(!n&&!o)&&(n||a.create(t.normalize("NFD").replace(/[\u0300-\u036f]/g,"").match(/[a-z]+/gi).map((e=>e.charAt(0).toUpperCase()+e.substr(1).toLowerCase())).join(""),o,r))}class a extends class{constructor(e,r){return new Proxy(this,{set(n,o,c){let i=e[o];const a=n[o],p=(l=a,!(JSON.stringify(c)===JSON.stringify(l)));var l;const d=f(i,o,r);function y(t,r){return Reflect.apply(t,n,r||[o,c,a,e])}if(!i){if(r.strict)return!0;i={}}if(y(n.onBeforeSet),p&&y(n.onBeforeUpdate),s(i.transform)&&(c=y(i.transform,[c,e])),r.exceptions){const e=u(i,o,c);if(e.length)throw new t({message:e[0].message,property:o,code:e[0].code,source:n.constructor.name})}return d&&(c=new d(c instanceof Object?c:{})),n[o]=c,y(n.onSet),p&&y(n.onUpdate),!0},get(t,r){const n=e[r];let o=t[r];if(!n)return t[r];function c(n,s){return Reflect.apply(n,t,s||[r,o,e])}return c(t.onBeforeGet),s(n)&&(o=c(n,[t,e])),s(n.format)&&(o=c(n.format,[o,e])),c(t.onGet),o},deleteProperty(r,n){const o=r[n];function s(t,s){return Reflect.apply(t,r,s||[n,o,e])}if(e[n].required)throw new t({message:`Invalid delete on required propery ${n}`,property:n,code:"required"});return s(r.onBeforeDelete),Reflect.deleteProperty(r,n),s(r.onDelete),s(r.onUpdate),!0}})}}{constructor(e={},t={},r){super(e,r),Object.keys(e).forEach((r=>{n(t[r])&&(this[r]=n(e[r].default)?t[r]:e[r].default)})),this.feed(t)}feed(e){Object.keys(e).forEach((t=>{this[t]=e[t]}))}onBeforeGet(){}onBeforeSet(){}onBeforeUpdate(){}onDelete(){}onGet(){}onBeforeDelete(){}onSet(){}onUpdate(){}}a.settings={strict:!1,exceptions:!0},a.create=function(e,t,n,o){n=Object.assign({},a.settings,n);const s={[e]:class extends a{constructor(e){super(t,e,n)}}}[e];return s.checkErrors=function(e,r){const o={};return Object.keys(t).forEach((s=>{let c;const a=e[s],p=t[s],l=f(p,s,n);l&&(c=l.checkErrors(a,r));let d=u(p,s,a);c?o[s]=c:d.length&&(r&&(d=d.filter((e=>!i(r).includes(e.code)))),d.length&&(o[s]=d))})),!!Object.keys(o).length&&o},s.hydrate=function(e){return r(e)?e.map((e=>new s(e))):new s(e)},Object.assign(s.prototype,o),s.schema=t,s},e.default=a,Object.defineProperty(e,"__esModule",{value:!0})},"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).SmartModel={});
/*! SmartModel v0.3.0 */
var e,t;e=this,t=function(){"use strict";class e extends Error{constructor(e){super(e.message),Object.assign(this,e)}}function t(e){return Array.isArray(e)}function r(e){return void 0===e}function n(e){return""===e||null===e||r(e)}function o(e){return"function"==typeof e}function s(e){return e.toString().startsWith("class")}function c(e){return[].concat([],e)}function i(e,r,o){const i=[];return e.required&&n(o)?(i.push({message:`Invalid value 'required' on property '${r}'`,code:"required"}),i):(void 0===o||(!e.type||!e.required&&n(o)||c(e.type).some((e=>function(e,r){const n=r&&r.toString().match(/^\s*function (\w+)/),o=(n?n[1]:"object").toLowerCase();if("date"===o&&e instanceof r)return!0;if("array"===o&&t(e))return!0;if("object"===o){if(s(r)&&e instanceof r)return!0;if(!s(r)&&typeof e===o)return!0}else if(typeof e===o)return!0;return!1}(o,e)))||i.push({message:`Invalid type '${typeof o}' on property '${r}'`,code:"type"}),e.rule&&Object.keys(e.rule).forEach((t=>{(0,e.rule[t])(o)&&i.push({message:`Invalid value '${t}' on property '${r}'`,code:t})}))),i)}function u(e={},t,r){if(!e.type)return!1;const n=e.type.prototype instanceof f&&e.type,o="[object Object]"===e.type.toString()&&e.type;return!(!n&&!o)&&(n||f.create(t.normalize("NFD").replace(/[\u0300-\u036f]/g,"").match(/[a-z]+/gi).map((e=>e.charAt(0).toUpperCase()+e.substr(1).toLowerCase())).join(""),o,r))}class f extends class{constructor(t,r){return new Proxy(this,{set(n,s,c){let f=t[s];const a=n[s],p=(l=a,!(JSON.stringify(c)===JSON.stringify(l)));var l;const d=u(f,s,r);function y(e,r){return Reflect.apply(e,n,r||[s,c,a,t])}if(!f){if(r.strict)return!0;f={}}if(y(n.onBeforeSet),p&&y(n.onBeforeUpdate),o(f.transform)&&(c=y(f.transform,[c,t])),r.exceptions){const t=i(f,s,c);if(t.length)throw new e({message:t[0].message,property:s,code:t[0].code,source:n.constructor.name})}return d&&(c=new d(c instanceof Object?c:{})),n[s]=c,y(n.onSet),p&&y(n.onUpdate),!0},get(e,r){const n=t[r];let s=e[r];if(!n)return e[r];function c(n,o){return Reflect.apply(n,e,o||[r,s,t])}return c(e.onBeforeGet),o(n)&&(s=c(n,[e,t])),o(n.format)&&(s=c(n.format,[s,t])),c(e.onGet),s},deleteProperty(r,n){const o=r[n];function s(e,s){return Reflect.apply(e,r,s||[n,o,t])}if(t[n].required)throw new e({message:`Invalid delete on required propery ${n}`,property:n,code:"required"});return s(r.onBeforeDelete),Reflect.deleteProperty(r,n),s(r.onDelete),s(r.onUpdate),!0}})}}{constructor(e={},t={},n){super(e,n),Object.keys(e).forEach((n=>{r(t[n])&&(this[n]=r(e[n].default)?t[n]:e[n].default)})),this.feed(t)}feed(e){Object.keys(e).forEach((t=>{this[t]=e[t]}))}onBeforeGet(){}onBeforeSet(){}onBeforeUpdate(){}onDelete(){}onGet(){}onBeforeDelete(){}onSet(){}onUpdate(){}}return f.settings={strict:!1,exceptions:!0},f.create=function(e,r,n,o){n=Object.assign({},f.settings,n);const s={[e]:class extends f{constructor(e){super(r,e,n)}}}[e];return s.checkErrors=function(e,t){const o={};return Object.keys(r).forEach((s=>{let f;const a=e[s],p=r[s],l=u(p,s,n);l&&(f=l.checkErrors(a,t));let d=i(p,s,a);f?o[s]=f:d.length&&(t&&(d=d.filter((e=>!c(t).includes(e.code)))),d.length&&(o[s]=d))})),!!Object.keys(o).length&&o},s.hydrate=function(e){return t(e)?e.map((e=>new s(e))):new s(e)},Object.assign(s.prototype,o),s.schema=r,s},f},"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).SmartModel=t();
{
"name": "@jaysalvat/smart-model",
"version": "0.2.15",
"version": "0.3.0",
"description": "Javascript object model",

@@ -5,0 +5,0 @@ "main": "./build/smart-model.umd.js",

@@ -15,2 +15,3 @@ [![npm version](https://badge.fury.io/js/%40jaysalvat%2Fsmart-model.svg)](https://badge.fury.io/js/%40jaysalvat%2Fsmart-model)

- [x] Virtual property
- [x] Throw exception (or not) if invalid
- [x] Nested models

@@ -20,5 +21,8 @@ - [x] Live cycle events

Works on modern browsers.
[Check if tests pass](https://unpkg.com/@jaysalvat/smart-model@latest/test/index.html) on your browser.
## Install
Install npm package
### NPM

@@ -29,4 +33,2 @@ ```sh

### Module
```javascript

@@ -36,6 +38,12 @@ import SmartModel from '@jaysalvat/smart-model'

### CDN
### Module from CDN
```javascript
import SmartModel from 'https://unpkg.com/@jaysalvat/smart-model@latest/build/smart-model.esm.min.js'
```
### The old way
```html
<script src="https://unpkg.com/@jaysalvat/smart-model@latest/build/smart-model.umd.min.js"></script>
<script src="https://unpkg.com/@jaysalvat/smart-model@latest/build/smart-model.min.js"></script>
```

@@ -48,36 +56,58 @@

```javascript
import readingTime from 'reading-time';
function readingtime(text) { /*...*/ }
const Post = SmartModel.create('Post', {
title: {
required: true,
type: String,
},
body: {
required: true,
type: String,
rule: {
'tooShort': (calue) => value.length < 100,
'tooLong': (calue) => value.length > 1000,
title: {
required: true,
type: String,
},
body: {
required: true,
type: String,
rule: {
'tooShort': (calue) => value.length < 100,
'tooLong': (calue) => value.length > 1000,
}
},
createdAt: {
type: Date,
default: new Date(),
transform: (value) => new Date(value),
format: (value) => value.toLocaleString()
},
updatedAt: {
type: Date,
default: new Date(),
transform: (value) => new Date(value),
format: (value) => value.toLocaleString()
},
// Nested model
author: {
required: true,
type: {
firstname: {
required: true,
type: String
},
lastname: {
type: String
}
}
},
// Virtual properties
bodyLength: (post) => post.body.length,
readingTime: (post) => readingTime(post.body)
},
// Settings
{
strict: false,
exceptions: true
}
// Events
{
onUpdate() {
this.updatedAt = new Date()
}
},
createdAt: {
type: Date,
default: new Date(),
transform: (value) => new Date(value),
format: (value) => value.toLocaleString()
},
updatedAt: {
type: Date,
default: new Date(),
transform: (value) => new Date(value),
format: (value) => value.toLocaleString()
},
bodyLength: (post) => post.body.length,
readingTime: (post) => readingTime(post.body)
}, {
onUpdate() {
this.updatedAt = new Date()
}
})
)
```

@@ -118,2 +148,16 @@

Tests
```sh
npm run test
```
```sh
npm run test:watch
```
```sh
npm run test:browser
```
Bump version and publish to NPM

@@ -123,5 +167,14 @@

npm run release
```
```sh
npm run release:patch
```
```sh
npm run release:minor
```
```sh
npm run release:major
```

@@ -13,3 +13,3 @@ /* eslint-disable camelcase */

const configs = []
const formats = [ 'esm', 'esm.min', 'umd', 'umd.min' ]
const formats = [ 'iife', 'iife.min', 'esm', 'esm.min', 'cjs', 'cjs.min', 'umd', 'umd.min', 'umd.min' ]
const mutedWarnings = [ 'CIRCULAR_DEPENDENCY' ]

@@ -54,3 +54,3 @@ const watched = process.env.ROLLUP_WATCH

const [ format, minify ] = type.split('.')
const filename = FILENAME + '.' + format + (minify ? '.min' : '') + '.js'
const filename = FILENAME + (format === 'iife' ? '' : '.' + format) + (minify ? '.min' : '') + '.js'

@@ -60,6 +60,6 @@ configs.push({

output: {
exports: 'named',
format: format,
file: DIST + '/' + filename,
name: format === 'umd' ? NAME : null,
name: NAME,
exports: 'default',
banner: !watched && (minify ? bannerMinify : bannerBeautify),

@@ -66,0 +66,0 @@ plugins: [

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc