Common Library Documentation

The common.js library provides a collection of small utility functions commonly used in SDK projects. It includes helpers for GUID generation, string escaping, array management, random string creation, and query parameter extraction.

Integration into your development project

{
  "javascript": {
    "eval": [
      "service/lib/*",
      "vendor-appname.js"
    ]
  }
}

Usage

All functions are attached to the global innovaphone.service.lib namespace and can be used directly after including the library into your makefile.

include service/lib1/common.mak

Methods

innovaphone.service.lib.generateGuid()

Generates a random GUID (UUID-like) string based on timestamp and random bytes.

var guid = innovaphone.service.lib.generateGuid();
log(guid); // e.g. "18b4a2d2fa8c-3a90-4f6b-8b2e-123456789abc"

innovaphone.service.lib.pick(obj, keys)

Creates a new object containing only the specified keys from the given object.

var original = { a: 1, b: 2, c: 3, d: 4 };
var picked = innovaphone.service.lib.pick(original, ["a", "c"]);
log(picked); // { a: 1, c: 3 }

innovaphone.service.lib.htmlentities(str)

Escapes HTML-sensitive characters in a string (useful for safe display in HTML).

var safe = innovaphone.service.lib.htmlentities('<script>alert("xss")</script>');
// Returns: "&lt;script&gt;alert(&quot;xss&quot;)&lt;/script&gt;"

innovaphone.service.lib.pushToArray(array, item, limit)

Pushes an item to an array and automatically removes the oldest entry if the array exceeds a given limit.

var logs = [];
innovaphone.service.lib.pushToArray(logs, "New entry", 10);

innovaphone.service.lib.getQueryParameter(url, parameter)

Extracts the value of a specific query parameter from a URL.

var value = innovaphone.service.lib.getQueryParameter("https://example.com?user=alice&id=42", "id");
log(value); // "42"

Example

This example shows how several functions from the Common Library can be used together:


    var guid = innovaphone.service.lib.generateGuid();
    var original = { a: 1, b: 2, c: 3, d: 4 };
    var picked = innovaphone.service.lib.pick(original, ["a", "c"]);
    var safeName = innovaphone.service.lib.htmlentities("<Alice>");
    var logs = [];

    innovaphone.service.lib.pushToArray(logs, "User " + safeName + " logged in", 5);

    log("GUID:", guid);
    log("Picked:", JSON.stringify(picked));
    log("Safe Name:", safeName);
    log("Logs:", logs);