Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
Loading items

Target

Select target project
  • oss/libraries/javascript/monster
1 result
Select Git revision
Loading items
Show changes
Showing
with 325 additions and 0 deletions
import {IsArray} from '@schukai/monster/source/constraints/isarray.mjs';
new IsArray()
.isValid([])
.then(() => console.log(true));
// ↦ true
new IsArray()
.isValid(99)
.catch(e => console.log(e));
// ↦ 99
\ No newline at end of file
import {IsObject} from '@schukai/monster/source/constraints/isobject.mjs';
new IsObject()
.isValid({})
.then(() => console.log(true));
// ↦ true
new IsObject()
.isValid(99)
.catch(e => console.log(e));
// ↦ 99
\ No newline at end of file
import {Valid} from '@schukai/monster/source/constraints/valid.mjs';
import {Invalid} from '@schukai/monster/source/constraints/invalid.mjs';
import {OrOperator} from '@schukai/monster/source/constraints/oroperator.mjs';
new OrOperator(
new Valid(), new Invalid()).isValid()
.then(() => console.log(true))
.catch(() => console.log(false));
// ↦ true
new OrOperator(
new Invalid(), new Invalid()).isValid()
.then(() => console.log(true))
.catch(() => console.log(false));
// ↦ false
\ No newline at end of file
import {Valid} from '@schukai/monster/source/constraints/valid.mjs';
new Valid().isValid()
.then(() => console.log(true))
.catch(() => console.log(false));
// ↦ true
import {buildMap} from '@schukai/monster/source/data/buildmap.mjs';
// a typical data structure as reported by an api
let map;
let obj = {
"data": [
{
"id": 10,
"name": "Cassandra",
"address": {
"street": "493-4105 Vulputate Street",
"city": "Saumur",
"zip": "52628"
}
},
{
"id": 20,
"name": "Holly",
"address": {
"street": "1762 Eget Rd.",
"city": "Schwalbach",
"zip": "952340"
}
},
{
"id": 30,
"name": "Guy",
"address": {
"street": "957-388 Sollicitudin Avenue",
"city": "Panchià",
"zip": "420729"
}
}
]
};
// The function is passed this data structure and with the help of the selector `'data.*'` the data to be considered are selected.
// The key is given by a simple definition `'id'` and the value is given by a template `'${name} (${address.zip} ${address.city})'`.
map = buildMap(obj, 'data.*', '${name} (${address.zip} ${address.city})', 'id');
console.log(map);
// ↦ Map(3) {
// '10' => 'Cassandra (52628 Saumur)',
// '20' => 'Holly (952340 Schwalbach)',
// '30' => 'Guy (420729 Panchià)'
// }
// If no key is specified, the key from the selection, here the array index, is taken.
map = buildMap(obj, 'data.*', '${name} (${address.zip} ${address.city})');
console.log(map);
// ↦ Map(3) {
// '0' => 'Cassandra (52628 Saumur)',
// '1' => 'Holly (952340 Schwalbach)',
// '2' => 'Guy (420729 Panchià)'
// }
// a filter (function(value, key) {}) can be specified to accept only defined entries.
map = buildMap(obj, 'data.*', '${name} (${address.zip} ${address.city})', 'id', function (value, key) {
return (value['id'] >= 20) ? true : false
});
console.log(map);
// ↦ Map(2) {
// 20 => 'Holly (952340 Schwalbach)',
// 30 => 'Guy (420729 Panchià)'
// }
\ No newline at end of file
import {Datasource} from '@schukai/monster/source/data/datasource.mjs'
class MyDatasource extends Datasource {
}
const ds = new MyDatasource();
import {Diff} from '@schukai/monster/source/data/diff.mjs';
// given are two objects x and y.
let x = {
a: 1,
b: "Hello!"
}
let y = {
a: 2,
c: true
}
// These two objects can be compared with each other.
console.log(Diff(x, y));
// the result is then the following
//
// [
// {
// operator: 'update',
// path: [ 'a' ],
// first: { value: 1, type: 'number' },
// second: { value: 2, type: 'number' }
// },
// {
// operator: 'delete',
// path: [ 'b' ],
// first: { value: 'Hello!', type: 'string' }
// },
// {
// operator: 'add',
// path: [ 'c' ],
// second: { value: true, type: 'boolean' }
// }
// ]
import {Pathfinder} from '@schukai/monster/source/data/pathfinder.mjs';
let value = new Pathfinder({
a: {
b: {
f: [
{
g: false,
}
],
}
}
}).getVia("a.b.f.0.g");
console.log(value);
// ↦ false
try {
new Pathfinder({}).getVia("a.b.f.0.g");
} catch (e) {
console.log(e.toString());
// ↦ Error: the journey is not at its end (b.f.0.g)
}
\ No newline at end of file
import {Pathfinder} from '@schukai/monster/source/data/pathfinder.mjs';
let p = new Pathfinder({
a: {
x: [
{c: 1}, {c: 2}
],
y: true
},
b: {
x: [
{c: 1, d: false}, {c: 2}
],
y: true
},
});
let r = p.getVia("*.x.*.c");
console.log(r);
import {Pipe} from '@schukai/monster/source/data/pipe.mjs';
let obj = {
a: {
b: {
c: {
d: "world"
}
}
}
}
console.log(new Pipe('path:a.b.c.d | toupper | prefix:Hello\\ ').run(obj));
// ↦ Hello WORLD
import {RestAPI} from '@schukai/monster/source/data/datasource/restapi.mjs';
const ds = new RestAPI({
url: 'https://httpbin.org/get'
}, {
url: 'https://httpbin.org/post'
});
ds.set({flag: true})
ds.write().then(() => console.log('done'));
ds.read().then(() => console.log('done'));
\ No newline at end of file
import {Transformer} from '@schukai/monster/source/data/transformer.mjs';
const transformer = new Transformer("tolower")
console.log(transformer.run("HELLO"))
// ↦ hello
console.log(transformer.run("WORLD"))
// ↦ world
\ No newline at end of file
import {CustomElement} from '@schukai/monster/source/dom/CustomElement';
// In the example the user can use his own template by creating a template in the DOM with the ID `my-custom-element`.
// You can also specify a theme (for example `mytheme`), then it will search for the ID `my-custom-element-mytheme` and
// if not available for the ID `my-custom-element`.
class MyCustomElement extends CustomElement {
static getTag() {
return "my-custom-element"
}
}
// ↦ <my-custom-element></my-custom-element>
\ No newline at end of file
import {getDocumentTheme} from '@schukai/monster/source/dom/theme.mjs';
const theme = getDocumentTheme();
console.log(theme.getName());
// ↦ monster
\ No newline at end of file
import {Updater} from '@schukai/monster/source/dom/updater.mjs';
// First we prepare the html document.
// This is done here via script, but can also be inserted into the document as pure html.
// To do this, simply insert the tag <h1 data-monster-replace="path:headline"></h1>.
const body = document.querySelector('body');
const headline = document.createElement('h1');
headline.setAttribute('data-monster-replace', 'path:headline')
body.appendChild(headline);
// the data structure
let obj = {
headline: "Hello World",
};
// Now comes the real magic. we pass the updater the parent HTMLElement
// and the desired data structure.
const updater = new Updater(body, obj);
updater.run();
// Now you can change the data structure and the HTML will follow these changes.
const subject = updater.getSubject();
subject['headline'] = "Hello World!"
\ No newline at end of file
import {Formatter} from '@schukai/monster/source/i18n/formatter.mjs';
import {Translations} from '@schukai/monster/source/i18n/translations.mjs';
const translations = new Translations('en')
.assignTranslations({
thekey: "${animal} has eaten the ${food}!"
});
new Formatter({}, translations).format("thekey:animal=dog::food=cake")
// ↦ dog has eaten the cake!
import {Fetch} from '@schukai/monster/source/i18n/providers/fetch.mjs';
// fetch from API
const translation = new Fetch('https://example.com/${language}.json').getTranslation('en-GB');
// ↦ https://example.com/en.json
import {Translations} from '@schukai/monster/source/i18n/translations.mjs';
import {parseLocale} from '@schukai/monster/source/i18n/locale.mjs';
const translation = new Translations(parseLocale('en-GB'));
translation.assignTranslations({
text1: "click",
text2: {
'one': 'click once',
'other': 'click n times'
}
});
console.log(translation.getText('text1'));
// ↦ click
console.log(translation.getPluralRuleText('text2', 1));
// -> click once
console.log(translation.getPluralRuleText('text2', 2));
// -> click n times
import {BaseWithOptions} from '@schukai/monster/source/types/basewithoptions.mjs';
class My extends BaseWithOptions {
get defaults() {
return Object.assign({}, super.defaults, {
mykey: true
});
}
}
import {isIterable} from '@schukai/monster/source/types/is.mjs';
isIterable(null) // ↦ false
isIterable('hello') // ↦ true
isIterable([]) // ↦ true
\ No newline at end of file