### Bootstrap Development Setup
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/README.md
Starts a development server that rebuilds the application on file changes. Ideal for active development.
```bash
npm run dev
```
--------------------------------
### Usage Example
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/os-homedir/readme.md
Use the os-homedir package to get the current user's home directory. This example demonstrates the basic import and function call.
```javascript
const osHomedir = require('os-homedir');
console.log(osHomedir());
//=> '/Users/sindresorhus'
```
--------------------------------
### Run Static File Server Example
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/http2/README.md
Execute the example static file server script located in the 'example' directory.
```bash
$ node ./example/server.js
```
--------------------------------
### Grunt Connect Server with Default Options
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/grunt-contrib-connect/README.md
Start a static web server using all default options by providing an empty configuration object for the target. This is useful for quick setups where default behavior is sufficient.
```js
// Project configuration.
grunt.initConfig({
connect: {
uses_defaults: {}
}
});
```
--------------------------------
### Install serve-static
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/serve-static/README.md
Install the serve-static module using npm.
```sh
$ npm install serve-static
```
--------------------------------
### Install to-fast-properties
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/to-fast-properties/readme.md
Install the package using npm.
```bash
$ npm install --save to-fast-properties
```
--------------------------------
### Install binary-extensions
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/binary-extensions/readme.md
Install the package using npm.
```bash
$ npm install binary-extensions
```
--------------------------------
### Example: Get Header Entries for Popup Menu
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/diagram-js/coverage/lcov-report/lib/features/popup-menu/PopupMenuProvider.js.html
Demonstrates how to create header entries for the popup menu. These can include buttons with specific actions and active states.
```javascript
PopupMenuProvider.getHeaderEntries = function(element) {};
```
```javascript
PopupMenuProvider.getHeaderEntries = function(element) {
var headerEntries = [{
id: 'my-button',
className: 'icon-button',
title: 'My button',
active: true,
action: function() {
alert('Button has been clicked');
}
}];
return headerEntries;
};
```
--------------------------------
### Install babel-plugin-syntax-async-functions
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/babel-plugin-syntax-async-functions/README.md
Install the plugin using npm.
```sh
$ npm install babel-plugin-syntax-async-functions
```
--------------------------------
### Install serve-index
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/serve-index/README.md
Install the serve-index module using npm.
```sh
$ npm install serve-index
```
--------------------------------
### Install process-nextick-args
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/process-nextick-args/readme.md
Install the process-nextick-args module using npm.
```bash
npm install --save process-nextick-args
```
--------------------------------
### Install on-finished
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/on-finished/README.md
Install the on-finished module using npm.
```sh
$ npm install on-finished
```
--------------------------------
### Install and Build Docs
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/base/node_modules/is-descriptor/README.md
Installs the necessary tools globally and then runs the verb command to generate documentation.
```sh
$ npm install -g verbose/verb#dev verb-generate-readme && verb
```
--------------------------------
### Install Portscanner with npm
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/portscanner/README.md
Install the portscanner module using npm. This is the initial setup step.
```bash
npm install portscanner
```
--------------------------------
### Get start segment bendpoints
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/diagram-js/coverage/lcov-report/lib/layout/ManhattanLayout.js.html
Calculates the bendpoints for the initial segment of a connection, starting from point 'a' towards point 'b'.
```javascript
function getStartSegment(a, b, directions) {
return getSegmentBendpoints(a, b, directions);
}
```
--------------------------------
### Install pkg-up
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/pkg-up/readme.md
Install the pkg-up module using npm.
```bash
$ npm install --save pkg-up
```
--------------------------------
### Example: Get Entries for Popup Menu
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/diagram-js/coverage/lcov-report/lib/features/popup-menu/PopupMenuProvider.js.html
Provides an example of how to define entries for the popup menu. Each entry has an id, label, class, and an action.
```javascript
PopupMenuProvider.getEntries = function(element) {};
```
```javascript
PopupMenuProvider.getEntries = function(element) {
var entries = [{
id: 'alert',
label: 'Alert element ID',
className: 'alert',
action: function () {
alert(element.id);
}
}];
return entries;
}
```
--------------------------------
### CLI Usage Example
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/browserify-incremental/README.md
Demonstrates basic command-line usage with react and output file specification. The second command shows the speed improvement after the initial build.
```bash
$ browserifyinc -r react -o output/bundle.js -v
556200 bytes written to output/bundle.js (1.38 seconds)
$ browserifyinc -r react -o output/bundle.js -v
556200 bytes written to output/bundle.js (0.13 seconds)
```
--------------------------------
### Install array-find-index
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/array-find-index/readme.md
Install the package using npm.
```bash
$ npm install --save array-find-index
```
--------------------------------
### Install spdx-license-ids
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/spdx-license-ids/README.md
Install the spdx-license-ids package using npm.
```bash
npm install spdx-license-ids
```
--------------------------------
### Run Command Line Client Example
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/http2/README.md
Use the example command-line client to download a file from the server. The downloaded file is saved to /tmp/server.js.
```bash
$ node ./example/client.js 'https://localhost:8080/server.js' >/tmp/server.js
```
--------------------------------
### Browser HTTP GET Request Example
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/stream-http/README.md
Demonstrates how to perform an HTTP GET request in the browser using the stream-http module and handle the response data.
```javascript
http.get('/bundle.js', function (res) {
var div = document.getElementById('result');
div.innerHTML += 'GET /beep
';
res.on('data', function (buf) {
div.innerHTML += buf;
});
res.on('end', function () {
div.innerHTML += '
__END__';
})
})
```
--------------------------------
### Install websocket-extensions
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/websocket-extensions/README.md
Install the websocket-extensions library using npm.
```bash
$ npm install websocket-extensions
```
--------------------------------
### Install and Help Command
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/dateformat/Readme.md
Installs the dateformat package and shows the help command.
```bash
$ npm install dateformat
$ dateformat --help
```
--------------------------------
### Enable Info Level Logging for Example Client
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/http2/README.md
Run the example client with 'info' level logging enabled by setting the HTTP2_LOG environment variable. Output is redirected to /dev/null.
```bash
$ HTTP2_LOG=info node ./example/client.js 'https://localhost:8080/server.js' >/dev/null
```
--------------------------------
### Dragging Integration Example
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/diagram-js/coverage/lcov-report/lib/features/dragging/Dragging.js.html
Example of integrating the dragging feature into a custom component using event bus listeners for drag start, move, and element clicks to initiate dragging.
```javascript
function MyDragComponent(eventBus, dragging) {
eventBus.on('mydrag.start', function(event) {
console.log('yes, we start dragging');
});
eventBus.on('mydrag.move', function(event) {
console.log('canvas local coordinates', event.x, event.y, event.dx, event.dy);
// local drag data is passed with the event
event.context.foo; // "BAR"
// the original mouse event, too
event.originalEvent; // MouseEvent(...)
});
eventBus.on('element.click', function(event) {
dragging.init(event, 'mydrag', {
cursor: 'grabbing',
data: {
context: {
foo: "BAR"
}
}
});
});
}
```
--------------------------------
### Array.find() and Array.findIndex() Examples
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/core-js/README.md
Use find() to get the first element that satisfies a provided testing function, and findIndex() to get its index. Returns undefined or -1 if no element is found.
```javascript
function isOdd(val){
return val % 2;
}
[4, 8, 15, 16, 23, 42].find(isOdd); // => 15
[4, 8, 15, 16, 23, 42].findIndex(isOdd); // => 2
[4, 8, 15, 16, 23, 42].find(isNaN); // => undefined
[4, 8, 15, 16, 23, 42].findIndex(isNaN); // => -1
```
--------------------------------
### Base.js Initialization with Config and Options
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/base/README.md
Illustrates initializing a Base instance with both configuration and options objects.
```javascript
// initialize with `config` and `options`
var app = new Base({isApp: true}, {abc: true});
app.set('foo', 'bar');
// values defined with the given `config` object will be on the root of the instance
console.log(app.baz); //=> undefined
console.log(app.foo); //=> 'bar'
// or use `.get`
console.log(app.get('isApp')); //=> true
console.log(app.get('foo')); //=> 'bar'
// values defined with the given `options` object will be on `app.options
console.log(app.options.abc); //=> true
```
--------------------------------
### Initiating the Simulator
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/hammerjs/tests/manual/simulator.html
Calls the function to start the gesture simulation process after all setup is complete.
```javascript
startSimulator();
```
--------------------------------
### Reflect.ownKeys Example
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/core-js/README.md
Demonstrates how to get all own property keys of an object, including Symbols, using Reflect.ownKeys.
```javascript
var O = {a: 1};
Object.defineProperty(O, 'b', {value: 2});
O[Symbol('c')] = 3;
Reflect.ownKeys(O); // => ['a', 'b', Symbol(c)]
```
--------------------------------
### Installing and Testing Connect
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/connect/README.md
Standard commands to install project dependencies and run the test suite.
```bash
npm install
npm test
```
--------------------------------
### Install read-pkg-up
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/read-pkg-up/readme.md
Install the read-pkg-up package using npm.
```bash
$ npm install --save read-pkg-up
```
--------------------------------
### Install and Hook Function in Node.js
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/hooker/README.md
Install the Hooker module using npm and then hook a function like Math.max to log the number of arguments passed. This example demonstrates basic usage in a Node.js environment.
```javascript
var hooker = require('hooker');
hooker.hook(Math, "max", function() {
console.log(arguments.length + " arguments passed");
});
Math.max(5, 6, 7) // logs: "3 arguments passed", returns 7
```
--------------------------------
### Run CLI Example
Source: https://github.com/cpn-io/cpn-js/blob/master/test/angular-tree-component/README.md
Run the CLI example to test your code locally. Ensure port 4200 is available before running.
```bash
$ npm run example:cli # Wait until webpack finishes and http://localhost:4200 is available
$ npm run test:dev
```
--------------------------------
### Install os-homedir
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/os-homedir/readme.md
Install the os-homedir package using npm.
```bash
$ npm install --save os-homedir
```
--------------------------------
### Array.fill() Example
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/core-js/README.md
Use fill() to populate an array with a static value from a start index to an end index.
```javascript
Array(5).fill(42); // => [42, 42, 42, 42, 42]
```
--------------------------------
### Build Documentation with verb
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/arr-union/README.md
Instructions for installing the verb package and running the documentation build process.
```sh
$ npm i verb && npm run docs
```
--------------------------------
### All Options in Use
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/load-grunt-tasks/readme.md
An example showcasing the usage of multiple options simultaneously: pattern, config, scope, and requireResolution.
```javascript
require('load-grunt-tasks')(grunt, {
pattern: 'grunt-contrib-*',
config: '../package.json',
scope: 'devDependencies',
requireResolution: true
});
```
--------------------------------
### Symbol.for and Symbol.keyFor Example
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/core-js/README.md
Illustrates how to use Symbol.for to create or retrieve a global symbol and Symbol.keyFor to get its key.
```javascript
var symbol = Symbol.for('key');
symbol === Symbol.for('key'); // true
Symbol.keyFor(symbol); // 'key'
```
--------------------------------
### Enable Info Level Logging for Example Server
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/http2/README.md
Run the example server with 'info' level logging enabled by setting the HTTP2_LOG environment variable.
```bash
$ HTTP2_LOG=info node ./example/server.js
```
--------------------------------
### Make an HTTP/2 Request
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/http2/README.md
Example of making a simple HTTP/2 GET request to a specified URL and piping the response to standard output.
```javascript
require('http2').get('https://localhost:8080/', function(response) {
response.pipe(process.stdout);
});
```
--------------------------------
### core-js Map Usage Examples
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/core-js/README.md
Demonstrates various ways to use the core-js Map, including setting, getting, checking size, and iterating.
```javascript
var a = [1];
var map = new Map([['a', 1], [42, 2]]);
map.set(a, 3).set(true, 4);
console.log(map.size); // => 4
console.log(map.has(a)); // => true
console.log(map.has([1])); // => false
console.log(map.get(a)); // => 3
map.forEach(function(val, key){
console.log(val); // => 1, 2, 3, 4
console.log(key); // => 'a', 42, [1], true
});
map.delete(a);
console.log(map.size); // => 3
console.log(map.get(a)); // => undefined
console.log(Array.from(map)); // => [['a', 1], [42, 2], [true, 4]]
```
```javascript
var map = new Map([['a', 1], ['b', 2], ['c', 3]]);
for(var [key, val] of map){
console.log(key); // => 'a', 'b', 'c'
console.log(val); // => 1, 2, 3
}
for(var val of map.values())console.log(val); // => 1, 2, 3
for(var key of map.keys())console.log(key); // => 'a', 'b', 'c'
for(var [key, val] of map.entries()){
console.log(key); // => 'a', 'b', 'c'
console.log(val); // => 1, 2, 3
}
```
--------------------------------
### Usage Example
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/resolve-pkg/readme.md
Demonstrates resolving a package path using `resolve-pkg` and contrasts it with `require.resolve()` when a package lacks a main entry point.
```js
const resolvePkg = require('resolve-pkg');
// $ npm install --save-dev grunt-svgmin
resolvePkg('grunt-svgmin/tasks', {cwd: __dirname});
//=> '/Users/sindresorhus/unicorn/node_modules/grunt-svgmin/tasks'
// fails here as grunt tasks usually don't have a defined main entry point
require.resolve('grunt-svgmin/tasks');
//=> Error: Cannot find module 'grunt-svgmin'
```
--------------------------------
### Object.keys and Object.getPrototypeOf Examples
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/core-js/README.md
Illustrates using Object.keys to get an array of an object's own enumerable property names and Object.getPrototypeOf to retrieve its prototype.
```javascript
Object.keys('qwe'); // => ['0', '1', '2']
Object.getPrototypeOf('qwe') === String.prototype; // => true
```
--------------------------------
### Run CLI Example (Windows)
Source: https://github.com/cpn-io/cpn-js/blob/master/test/angular-tree-component/README.md
Alternative command for Windows users to run the CLI example if the standard command fails.
```bash
npm run example:cli:win
```
--------------------------------
### Get Docking Point
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/diagram-js/coverage/lcov-report/lib/layout/ConnectionDocking.js.html
Determines the docking point for a connection on a given shape. It identifies the start or end waypoint based on the dockStart flag.
```javascript
ConnectionDocking.prototype.getDockingPoint = function(connection, shape, dockStart) {
var waypoints = connection.waypoints,
dockingIdx,
dockingPoint;
dockingIdx = dockStart ? 0 : waypoints.length - 1;
dockingPoint = waypoints[dockingIdx];
return {
point: dockingPoint,
actual: dockingPoint,
idx: dockingIdx
};
};
```
--------------------------------
### Using Base.js Plugins
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/base/README.md
Demonstrates how to create and use a simple plugin with a Base instance.
```javascript
var base = new Base();
function plugin(base) {
// do plugin stuff, in pure JavaScript
}
// use the plugin
base.use(plugin);
```
--------------------------------
### RANGE Token Example
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/ret/README.md
The RANGE token is used within SET tokens to define a character range, specifying the start and end character codes.
```json
{
"type": ret.types.RANGE,
"from": 97,
"to": 122,
}
```
--------------------------------
### Create an HTTP/2 Server
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/http2/README.md
Example of creating a basic HTTP/2 server that responds with 'Hello world!' to all requests. Requires SSL key and certificate files.
```javascript
var options = {
key: fs.readFileSync('./example/localhost.key'),
cert: fs.readFileSync('./example/localhost.crt')
};
require('http2').createServer(options, function(request, response) {
response.end('Hello world!');
}).listen(8080);
```
--------------------------------
### Generate documentation with verb
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/is-extglob/README.md
Install verb and verb-generate-readme globally, then run verb to generate the documentation.
```sh
$ npm install -g verb verb-generate-readme && verb
```
--------------------------------
### Lo-Dash-FP/Ramda Integration
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/underscore.string/README.markdown
Use underscore.string with functional programming libraries like Ramda or Lo-Dash-FP by installing and requiring underscore.string.fp. This example demonstrates filtering an array of strings.
```javascript
var S = require('underscore.string.fp');
var filter = require('lodash-fp').filter;
var filter = require('ramda').filter;
filter(S.startsWith('.'), [
'.vimrc',
'foo.md',
'.zshrc'
]);
// => ['.vimrc', '.zshrc']
```
--------------------------------
### Set and Map .of and .from Examples
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/core-js/README.md
Shows how to create Sets and Maps using the .of and .from static methods, including mapping functions for .from.
```javascript
Set.of(1, 2, 3, 2, 1); // => Set {1, 2, 3}
Map.from([[1, 2], [3, 4]], ([key, val]) => [key ** 2, val ** 2]); // => Map {1: 4, 9: 16}
```
--------------------------------
### Get Last Child Node
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/snapdragon-node/README.md
Retrieves the last child node in the node's children list. Returns undefined if the node has no children. This example is repeated.
```javascript
var foo = new Node({type: 'foo'});
var bar = new Node({type: 'bar'});
var baz = new Node({type: 'baz'});
var qux = new Node({type: 'qux'});
foo.push(bar);
foo.push(baz);
foo.push(qux);
console.log(foo.last.type) // 'qux'
```
--------------------------------
### core-js WeakMap Usage Examples
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/core-js/README.md
Demonstrates core-js WeakMap usage, including setting, getting, and checking for keys, as well as using it for private property storage.
```javascript
var a = [1]
, b = [2]
, c = [3];
var wmap = new WeakMap([[a, 1], [b, 2]]);
wmap.set(c, 3).set(b, 4);
console.log(wmap.has(a)); // => true
console.log(wmap.has([1])); // => false
console.log(wmap.get(a)); // => 1
wmap.delete(a);
console.log(wmap.get(a)); // => undefined
```
```javascript
// Private properties store:
var Person = (function(){
var names = new WeakMap;
function Person(name){
names.set(this, name);
}
Person.prototype.getName = function(){
return names.get(this);
};
return Person;
})();
var person = new Person('Vasya');
console.log(person.getName()); // => 'Vasya'
for(var key in person)console.log(key); // => only 'getName'
```
--------------------------------
### Component Installation
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/isarray/README.md
Install the isarray package using component for front-end projects.
```bash
$ component install juliangruber/isarray
```
--------------------------------
### Example HTML Structure
Source: https://github.com/cpn-io/cpn-js/blob/master/test/test-diagram-js/node_modules/vm-browserify/readme.markdown
This HTML sets up the basic structure for the example, including loading jQuery and the bundled JavaScript file.
```html