Comparing version 1.0.30 to 1.0.31
@@ -6,3 +6,3 @@ /** | ||
let model = new Lanurite.Model({name: "Yura"}); | ||
let model = new Lanurite.Model({name: "Robot"}); | ||
document.getElementById("test").innerHTML = model.get("name"); | ||
@@ -12,9 +12,5 @@ | ||
document.getElementById("test").innerHTML = model.get("name") | ||
if (model.get("name") == "Yura") { | ||
return alert("Privet") | ||
} | ||
return alert("Papa") | ||
model.off("change", log) | ||
}); | ||
}) | ||
let collection = new Lanurite.Collection(); | ||
@@ -30,2 +26,4 @@ | ||
model.on("change", log); | ||
collection.on("reset", function () { | ||
@@ -37,2 +35,6 @@ getCollectionDOM(); | ||
function log(event){ | ||
console.log(event) | ||
} | ||
function getCollectionDOM() { | ||
@@ -39,0 +41,0 @@ document.getElementById("collection").innerHTML = ""; |
@@ -12,3 +12,10 @@ { | ||
"client", | ||
"browser" | ||
"browser", | ||
"nodejs", | ||
"typescript", | ||
"angular", | ||
"vuejs", | ||
"react", | ||
"meteor", | ||
"events" | ||
], | ||
@@ -23,7 +30,9 @@ "author": "Yuriy Panarin", | ||
"ts-loader": "2.3.3", | ||
"webpack": "3.5.5" | ||
"webpack": "3.5.5", | ||
"typedoc": "0.8.0", | ||
"typedoc-webpack-plugin": "1.1.4" | ||
}, | ||
"main": "dist/lanurite.js", | ||
"typings": "src/lanurite.d.ts", | ||
"version": "1.0.30", | ||
"version": "1.0.31", | ||
"license": "MIT", | ||
@@ -30,0 +39,0 @@ "repository": { |
# Lanurite | ||
Library for Models and Collection use in JS worlds | ||
[![NPM](https://nodei.co/npm/lanurite.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/lanurite/) | ||
**For use in browser** | ||
`npm install .` | ||
`webpack` | ||
`<script src="../dist/lanurite.js"></script>` | ||
@@ -22,4 +19,48 @@ | ||
**For use in NodeJS** | ||
``` | ||
var t = require("lanurite.js"); | ||
console.log(t.version) | ||
var Lanurite = require("lanurite"); | ||
console.log(Lanurite.version) | ||
``` | ||
## Example | ||
```javascript | ||
let model = new Lanurite.Model({name: "Robot"}); | ||
document.getElementById("test").innerHTML = model.get("name"); | ||
model.on("change", function (e) { | ||
document.getElementById("test").innerHTML = model.get("name") | ||
model.off("change", log) | ||
}); | ||
let collection = new Lanurite.Collection(); | ||
collection.on("add", function (model) { | ||
document.getElementById("collection").innerHTML += model.get("name") + "<br>" | ||
}) | ||
collection.on("clear", function () { | ||
document.getElementById("collection").innerHTML = ""; | ||
}); | ||
model.on("change", log); | ||
collection.on("reset", function () { | ||
getCollectionDOM(); | ||
}); | ||
getCollectionDOM(); | ||
function log(event){ | ||
console.log(event) | ||
} | ||
function getCollectionDOM() { | ||
document.getElementById("collection").innerHTML = ""; | ||
collection.each((el) => { | ||
document.getElementById("collection").innerHTML += el.get("name") + "<br>" | ||
}); | ||
} | ||
``` |
@@ -9,25 +9,118 @@ /// <reference types="lodash" /> | ||
constructor(array?: Array<any>); | ||
_init(array: Array<any>): void; | ||
private _init(array); | ||
/** | ||
* Add model in collection, return True if model added or False if model already in collection | ||
* @param model | ||
* @returns {boolean} | ||
*/ | ||
add(model: IModel): boolean; | ||
/** | ||
* Remove model from collection, return true if model removed or False if model not in collection | ||
* @param model | ||
* @returns {boolean} | ||
*/ | ||
remove(model: IModel): boolean; | ||
/** | ||
* Return existing model in collection | ||
* @param model | ||
* @returns {boolean} | ||
*/ | ||
has(model: IModel): boolean; | ||
/** | ||
* Clear collection, remove all models, event will be save | ||
*/ | ||
clear(): void; | ||
/** | ||
* Filtering collection with predicate | ||
* @param predicate - function | ||
* @returns {Array<any>} | ||
*/ | ||
filter(predicate: any): Array<any>; | ||
/** | ||
* Create new array with predicate | ||
* @param predicate - function | ||
* @returns {Array<any>} | ||
*/ | ||
map(predicate: any): Array<any>; | ||
/** | ||
* Get Model by id | ||
* @param id | ||
* @returns Model | null | ||
*/ | ||
getById(id: string): any; | ||
/** | ||
* Find first model filtering by predicate | ||
* @param predicate - function | ||
* @param startIndex - number start index | ||
* @returns {Model|undefined} | ||
*/ | ||
find(predicate: any, startIndex?: number): any; | ||
/** | ||
* Accumulation collection with predicate | ||
* @param predicate - function | ||
* @param accum | ||
* @returns {any} | ||
*/ | ||
reduce(predicate: any, accum?: any): any; | ||
/** | ||
* Return Array of Model | ||
* @returns {Array[Model]} | ||
*/ | ||
getAll(): Array<any>; | ||
_isModel(object: any): boolean; | ||
private _isModel(object); | ||
/** | ||
* Iterate collection with predicate | ||
* @param predicate - function | ||
*/ | ||
each(predicate: any): void; | ||
/** | ||
* Merge collection with Array<any> or Array<Model> | ||
* @param collection | ||
*/ | ||
merge(collection: Array<any> | ICollection): void; | ||
/** | ||
* Reset collection with new data array, events will be save | ||
* @param array | ||
*/ | ||
reset(array?: Array<any>): void; | ||
_clearCollection(): void; | ||
private _clearCollection(); | ||
/** | ||
* Get collection length | ||
* @returns {number} | ||
*/ | ||
getLength(): number; | ||
/** | ||
* Get JSON from collection | ||
* @returns {Array<any>} | ||
*/ | ||
toJSON(): any[]; | ||
/** | ||
* Sorting collection with predicate, changing collection order. | ||
* @param predicate - function | ||
*/ | ||
sortBy(predicate: any): void; | ||
/** | ||
* Return Array of Model | ||
* @returns {Array<any>} | ||
*/ | ||
toArray(): Array<any>; | ||
chunk(size?: number): any; | ||
/** | ||
* Creates an collection of elements split into groups the length of size. If array can't be split evenly, the final chunk will be the remaining elements. | ||
* @param size | ||
* @returns {Array<any>} | ||
*/ | ||
chunk(size?: number): Array<any>; | ||
/** | ||
* Creates an object composed of keys generated from the results of running each element of collection thru iteratee. The corresponding value of each key is the number of times the key was returned by iteratee. | ||
* @param predicate - function | ||
* @returns {Dictionary<number>} | ||
*/ | ||
countBy(predicate: any): _.Dictionary<number>; | ||
/** | ||
* Creates an object composed of keys generated from the results of running each element of collection thru iteratee. The order of grouped values is determined by the order they occur in collection. The corresponding value of each key is an array of elements responsible for generating the key. | ||
* @param predicate - function | ||
* @returns {Dictionary<T[]>} | ||
*/ | ||
groupBy(predicate: any): _.Dictionary<any[]>; | ||
} | ||
export { Collection }; |
@@ -16,3 +16,3 @@ import * as _ from "lodash"; | ||
_init(array: Array<any>) { | ||
private _init(array: Array<any>) { | ||
array.forEach((object) => { | ||
@@ -27,2 +27,8 @@ if (this._isModel(object)) { | ||
/** | ||
* Add model in collection, return True if model added or False if model already in collection | ||
* @param model | ||
* @returns {boolean} | ||
*/ | ||
add(model: IModel) { | ||
@@ -37,2 +43,8 @@ if (_.isUndefined(this._models[model.get("l_id")])) { | ||
/** | ||
* Remove model from collection, return true if model removed or False if model not in collection | ||
* @param model | ||
* @returns {boolean} | ||
*/ | ||
remove(model: IModel) { | ||
@@ -47,2 +59,7 @@ if (!_.isUndefined(this._models[model.get("l_id")])) { | ||
/** | ||
* Return existing model in collection | ||
* @param model | ||
* @returns {boolean} | ||
*/ | ||
@@ -53,2 +70,6 @@ has(model: IModel) { | ||
/** | ||
* Clear collection, remove all models, event will be save | ||
*/ | ||
clear() { | ||
@@ -59,2 +80,8 @@ this._clearCollection(); | ||
/** | ||
* Filtering collection with predicate | ||
* @param predicate - function | ||
* @returns {Array<any>} | ||
*/ | ||
filter(predicate: any): Array<any> { | ||
@@ -64,2 +91,8 @@ return this.getAll().filter(predicate) | ||
/** | ||
* Create new array with predicate | ||
* @param predicate - function | ||
* @returns {Array<any>} | ||
*/ | ||
map(predicate: any): Array<any> { | ||
@@ -69,2 +102,8 @@ return this.getAll().map(predicate) | ||
/** | ||
* Get Model by id | ||
* @param id | ||
* @returns Model | null | ||
*/ | ||
getById(id: string) { | ||
@@ -77,2 +116,9 @@ if (!_.isUndefined(this._models[id])) { | ||
/** | ||
* Find first model filtering by predicate | ||
* @param predicate - function | ||
* @param startIndex - number start index | ||
* @returns {Model|undefined} | ||
*/ | ||
find(predicate: any, startIndex: number = 0): any { | ||
@@ -82,2 +128,9 @@ return _.find(this.getAll(), predicate, startIndex); | ||
/** | ||
* Accumulation collection with predicate | ||
* @param predicate - function | ||
* @param accum | ||
* @returns {any} | ||
*/ | ||
reduce(predicate: any, accum: any = 0): any { | ||
@@ -87,2 +140,7 @@ return this.getAll().reduce(predicate, accum) | ||
/** | ||
* Return Array of Model | ||
* @returns {Array[Model]} | ||
*/ | ||
getAll(): Array<any> { | ||
@@ -92,10 +150,20 @@ return _.values(this._models) | ||
_isModel(object: any) { | ||
private _isModel(object: any) { | ||
return object instanceof Model | ||
} | ||
each(predicate: any){ | ||
/** | ||
* Iterate collection with predicate | ||
* @param predicate - function | ||
*/ | ||
each(predicate: any) { | ||
return this.getAll().forEach(predicate) | ||
} | ||
/** | ||
* Merge collection with Array<any> or Array<Model> | ||
* @param collection | ||
*/ | ||
merge(collection: Array<any> | ICollection) { | ||
@@ -117,2 +185,6 @@ if (_.isArray(collection)) { | ||
/** | ||
* Reset collection with new data array, events will be save | ||
* @param array | ||
*/ | ||
reset(array: Array<any> = []) { | ||
@@ -124,3 +196,3 @@ this._clearCollection(); | ||
_clearCollection() { | ||
private _clearCollection() { | ||
Object.keys(this._models).forEach((key) => { | ||
@@ -131,2 +203,6 @@ delete this._models[key] | ||
/** | ||
* Get collection length | ||
* @returns {number} | ||
*/ | ||
getLength(): number { | ||
@@ -136,4 +212,9 @@ return Object.keys(this._models).length | ||
toJSON(){ | ||
return this.map((el) =>{ | ||
/** | ||
* Get JSON from collection | ||
* @returns {Array<any>} | ||
*/ | ||
toJSON() { | ||
return this.map((el) => { | ||
return el.toJSON() | ||
@@ -143,17 +224,46 @@ }) | ||
sortBy(predicate: any){ | ||
/** | ||
* Sorting collection with predicate, changing collection order. | ||
* @param predicate - function | ||
*/ | ||
sortBy(predicate: any) { | ||
this.reset(this.getAll().sort(predicate)) | ||
} | ||
toArray(): Array<any>{ | ||
/** | ||
* Return Array of Model | ||
* @returns {Array<any>} | ||
*/ | ||
toArray(): Array<any> { | ||
return this.getAll() | ||
} | ||
chunk(size: number = 1): any{ | ||
/** | ||
* Creates an collection of elements split into groups the length of size. If array can't be split evenly, the final chunk will be the remaining elements. | ||
* @param size | ||
* @returns {Array<any>} | ||
*/ | ||
chunk(size: number = 1): Array<any> { | ||
return _.chunk(this.getAll(), size) | ||
} | ||
countBy(predicate: any){ | ||
/** | ||
* Creates an object composed of keys generated from the results of running each element of collection thru iteratee. The corresponding value of each key is the number of times the key was returned by iteratee. | ||
* @param predicate - function | ||
* @returns {Dictionary<number>} | ||
*/ | ||
countBy(predicate: any) { | ||
return _.countBy(this.getAll(), predicate); | ||
} | ||
/** | ||
* Creates an object composed of keys generated from the results of running each element of collection thru iteratee. The order of grouped values is determined by the order they occur in collection. The corresponding value of each key is an array of elements responsible for generating the key. | ||
* @param predicate - function | ||
* @returns {Dictionary<T[]>} | ||
*/ | ||
groupBy(predicate: any) { | ||
return _.groupBy(this.getAll(), predicate); | ||
} | ||
} | ||
@@ -160,0 +270,0 @@ |
import { IEvent } from "../interfaces/IEvent"; | ||
declare class Event implements IEvent { | ||
_events: any; | ||
protected _events: any; | ||
on(eventName: string, handler: any): any[]; | ||
off(eventName: string): void; | ||
off(eventName: string, handler?: any): boolean; | ||
trigger(eventName: string, eventParams?: any): void; | ||
} | ||
export { Event }; |
import * as _ from "lodash"; | ||
import {IEvent} from "../interfaces/IEvent"; | ||
class Event implements IEvent{ | ||
class Event implements IEvent { | ||
public _events: any = {}; | ||
protected _events: any = {}; | ||
@@ -14,4 +14,10 @@ on(eventName: string, handler: any) { | ||
off(eventName: string) { | ||
delete this._events[eventName]; | ||
off(eventName: string, handler?: any) { | ||
if (handler && handler.name.length) { | ||
this._events[eventName] = this._events[eventName].filter((callback) => { | ||
return callback.name !== handler.name | ||
}); | ||
return; | ||
} | ||
return delete this._events[eventName]; | ||
} | ||
@@ -18,0 +24,0 @@ |
@@ -6,7 +6,32 @@ import { IModel } from "../interfaces/IModel"; | ||
constructor(obj?: any); | ||
get(key: string): any; | ||
set(key: string, value: any): void; | ||
hasProperty(key: string): boolean; | ||
/** | ||
* Get value from key in Model | ||
* @param key | ||
* @returns {any} | ||
*/ | ||
get(key: string | number): any; | ||
/** | ||
* Set value by key in Model | ||
* @param key | ||
* @param value | ||
*/ | ||
set(key: string | number, value: any): void; | ||
/** | ||
* Checking existing key in model | ||
* @param key | ||
* @returns {boolean} | ||
*/ | ||
has(key: string | number): boolean; | ||
/** | ||
* Return JSON from Model | ||
* @returns {T} | ||
*/ | ||
toJSON(): any; | ||
/** | ||
* Drop value by key in Model | ||
* @param key | ||
* @returns {boolean} | ||
*/ | ||
drop(key: string | number): boolean; | ||
} | ||
export { Model }; |
import * as _ from "lodash"; | ||
import {IModel} from "../interfaces/IModel"; | ||
import {Event} from "./Event" | ||
class Model extends Event implements IModel{ | ||
class Model extends Event implements IModel { | ||
@@ -13,9 +13,19 @@ private _model: any; | ||
/** | ||
* Get value from key in Model | ||
* @param key | ||
* @returns {any} | ||
*/ | ||
get(key: string) { | ||
get(key: string | number) { | ||
return this._model[key] | ||
} | ||
set(key: string, value: any) { | ||
if (this.hasProperty(key)) { | ||
/** | ||
* Set value by key in Model | ||
* @param key | ||
* @param value | ||
*/ | ||
set(key: string | number, value: any) { | ||
if (this.has(key)) { | ||
let oldValue = this.get(key) | ||
@@ -30,11 +40,34 @@ this._model[key] = value; | ||
hasProperty(key: string) { | ||
/** | ||
* Checking existing key in model | ||
* @param key | ||
* @returns {boolean} | ||
*/ | ||
has(key: string | number) { | ||
return !_.isUndefined(this._model[key]) | ||
} | ||
toJSON(): any{ | ||
/** | ||
* Return JSON from Model | ||
* @returns {T} | ||
*/ | ||
toJSON(): any { | ||
return _.clone(this._model); | ||
} | ||
/** | ||
* Drop value by key in Model | ||
* @param key | ||
* @returns {boolean} | ||
*/ | ||
drop(key: string | number){ | ||
if (this.has(key)){ | ||
delete this._model[key]; | ||
return true | ||
} | ||
return false | ||
} | ||
} | ||
export {Model} |
import { IModel } from "./IModel"; | ||
import { Collection } from "../classes/Collection"; | ||
export interface ICollection { | ||
@@ -9,3 +8,3 @@ add(model: IModel): boolean; | ||
getAll(): Array<any>; | ||
merge(collection: Array<any> | Collection): void; | ||
merge(collection: Array<any> | ICollection): void; | ||
filter(predicate: any): Array<any>; | ||
@@ -22,4 +21,5 @@ map(predicate: any): Array<any>; | ||
toArray(): Array<any>; | ||
chunk(size: number): any; | ||
chunk(size: number): Array<any>; | ||
countBy(predicate: any): any; | ||
groupBy(predicate: any): any; | ||
} |
import {IModel} from "./IModel"; | ||
import {IEvent} from "./IEvent"; | ||
import {Collection} from "../classes/Collection"; | ||
export interface ICollection { | ||
@@ -10,3 +9,3 @@ add(model: IModel): boolean | ||
getAll(): Array<any> | ||
merge(collection: Array<any> | Collection): void | ||
merge(collection: Array<any> | ICollection): void | ||
filter(predicate: any): Array<any> | ||
@@ -23,4 +22,5 @@ map(predicate: any): Array<any> | ||
toArray(): Array<any> | ||
chunk(size: number): any | ||
chunk(size: number): Array<any> | ||
countBy(predicate: any): any | ||
groupBy(predicate: any): any | ||
} |
export interface IModel { | ||
get(property: string | number): any; | ||
set(property: string | number, value: any): void; | ||
hasProperty(property: string | number): boolean; | ||
has(property: string | number): boolean; | ||
drop(property: string | number): boolean; | ||
toJSON(): any; | ||
} |
@@ -1,6 +0,7 @@ | ||
export interface IModel{ | ||
export interface IModel { | ||
get(property: string | number): any; | ||
set(property: string | number, value: any): void | ||
hasProperty(property: string | number): boolean | ||
has(property: string | number): boolean | ||
drop(property: string | number): boolean | ||
toJSON(): any | ||
} |
let webpack = require("webpack"); | ||
let TypedocWebpackPlugin = require('typedoc-webpack-plugin'); | ||
@@ -31,5 +32,12 @@ module.exports = { | ||
}), | ||
new webpack.optimize.UglifyJsPlugin({minimize: true}) | ||
new webpack.optimize.UglifyJsPlugin({minimize: true}), | ||
new TypedocWebpackPlugin({ | ||
out: './docs', | ||
module: 'umd', | ||
exclude: '**/node_modules/**/*.*', | ||
experimentalDecorators: true, | ||
excludeExternals: true | ||
}) | ||
] | ||
}; | ||
Sorry, the diff of this file is too big to display
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 2 instances in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
795727
51
5006
66
4
2
4