Skip to content

Instantly share code, notes, and snippets.

View tswistak's full-sized avatar

Tomasz Świstak tswistak

View GitHub Profile
@tswistak
tswistak / snippets.js
Created February 9, 2018 09:33
Quick JS collections snippets
/*
* Snippets collected during development: made by myself or found on StackOverflow/GitHub.
* Collection will grow in time.
* Enjoy.
*/
// --- removing duplicates from an array ---
[2, 4, 5, 2, 6, 4].filter((el, i, arr) => arr.indexOf(el) === i);
[2, 4, 5, 2, 6, 4].reduce((acc, curr) => acc.includes(curr) ? acc : [...acc, curr], []);
[2, 4, 5, 2, 6, 4].reduce((acc, curr) => acc.includes(curr) ? acc : acc.concat(curr), []);
/*
* Snippets collected during development: made by myself or found on StackOverflow/GitHub.
* Collection will grow in time.
* Enjoy.
*/
// --- running each function in an array ---
const tab = [(a) => console.log(a),
(a) => console.log(a+a),
(a) => console.log(a-a)];
@tswistak
tswistak / execute-function.js
Created March 15, 2018 08:12
Execute function only if defined
const a = undefined;
// 1
if (typeof a === 'Function') a();
// 1 with jQuery
if ($.isFunction(a)) a();
// same in Lodash -> _.isFunction
// 2
@tswistak
tswistak / index.html
Last active June 4, 2018 09:17
GoJS tutorial part 1, listing 1
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gojs/1.8.12/go.js"></script>
</head>
<body>
<div id="diagram-content" style="height: 800px; border: 1px solid black;"></div>
</body>
</html>
@tswistak
tswistak / diagram.js
Last active June 4, 2018 09:27
GoJS tutorial part 1, listing 2
diagram = (function(){
var $ = go.GraphObject.make;
var diagram;
var initDiagram = function () {
diagram = $(go.Diagram,
'diagram-content', {
initialContentAlignment: go.Spot.Center
});
}
return { initDiagram };
@tswistak
tswistak / diagram.js
Created June 4, 2018 09:29
GoJS tutorial part 1, listing 3
diagram.model = $(go.GraphLinksModel, {
nodeDataArray: [
{key: 1, category: 'first'},
{key: 2, category: 'second'},
{key: 3, category: 'second'},
{key: 4, category: 'third'}
],
linkDataArray: [
{from: 1, to: 2},
{from: 2, to: 3},
@tswistak
tswistak / diagram.js
Created June 4, 2018 09:45
GoJS tutorial part 1, listing 4
diagram.nodeTemplate = $(go.Node, 'Auto', $(go.Shape, 'Circle'));
@tswistak
tswistak / diagram.js
Created June 4, 2018 09:49
GoJS tutorial part 1, listing 5
diagram.nodeTemplate = $(go.Node,
'Auto',
$(go.Shape, {
geometryString: 'F M0 0 L100 0 Q150 50 100 100 L0 100 Q50 50 0 0z',
fill: 'white',
width: 100,
height: 100
}));
@tswistak
tswistak / diagram.js
Created June 4, 2018 09:51
GoJS tutorial part 1, listing 6
diagram.nodeTemplate = $(go.Node,
'Vertical',
$(go.Shape,
'Rectangle', {
width: 50,
height: 50,
strokeWidth: 0,
fill: 'yellow',
}),
$(go.Shape,
@tswistak
tswistak / diagram.js
Created June 4, 2018 09:53
GoJS tutorial part 1, listing 7
var getTemplates = function () {
return [{
category: 'first',
template: $(go.Node, 'Auto', $(go.Shape, 'Circle'))
}, {
category: 'second',
template: $(go.Node,
'Auto',
$(go.Shape, {
// ...