Google's comprehensive JavaScript library providing utilities, DOM manipulation, UI components, and data structures for building scalable web applications
npx @tessl/cli install tessl/npm-google-closure-library@20230802.0.0Google Closure Library is a comprehensive JavaScript library providing a robust set of utilities, DOM manipulation tools, event handling, UI components, and data structures for building complex and scalable web applications. Used by major Google applications including Gmail, Google Docs, and Google Maps, it offers cross-browser compatibility, extensive testing frameworks, and optimized performance for enterprise-scale development.
npm install google-closure-libraryThe library uses a unique module system with goog.require() and goog.provide():
Node.js Bootstrap:
require('google-closure-library/closure/goog/bootstrap/nodejs');Core Base System:
// After bootstrap, use the goog namespace
goog.require('goog.array');
goog.require('goog.dom');
goog.require('goog.events');// Bootstrap the library for Node.js
require('google-closure-library/closure/goog/bootstrap/nodejs');
// Require needed modules
goog.require('goog.dom');
goog.require('goog.events');
goog.require('goog.array');
// DOM manipulation
const element = goog.dom.getElement('myDiv');
goog.dom.setTextContent(element, 'Hello Closure!');
// Array utilities
const numbers = [1, 2, 3, 4, 5];
const doubled = goog.array.map(numbers, n => n * 2);
const evens = goog.array.filter(doubled, n => n % 2 === 0);
// Event handling
goog.events.listen(element, 'click', function(e) {
console.log('Element clicked!');
});Google Closure Library is built around several key architectural principles:
goog.provide() and goog.require() for dependency managementgoog namespace with logical sub-namespacesThe fundamental module system, object utilities, type checking, and class inheritance system that powers the entire library.
// Module system
goog.provide(name);
goog.require(name);
// Type checking
goog.isDefAndNotNull(val);
goog.isFunction(val);
goog.isObject(val);
goog.isArray(val);
goog.isString(val);
// Class system
goog.inherits(childCtor, parentCtor);
goog.bind(fn, selfObj, ...args);Comprehensive collection of array manipulation functions including iteration, search, modification, sorting, and set operations.
// Iteration
goog.array.forEach(arr, f, opt_obj);
goog.array.map(arr, f, opt_obj);
goog.array.filter(arr, f, opt_obj);
// Search and access
goog.array.indexOf(arr, obj, opt_fromIndex);
goog.array.find(arr, f, opt_obj);
goog.array.contains(arr, obj);
// Modification
goog.array.insert(arr, obj);
goog.array.remove(arr, obj);
goog.array.removeAt(arr, i);Powerful DOM manipulation utilities for element access, creation, content management, and tree traversal with cross-browser compatibility.
// Element access
goog.dom.getElement(element);
goog.dom.getElementsByClass(className, opt_el);
goog.dom.$(id);
// Element creation
goog.dom.createElement(tagName);
goog.dom.createDom(tagName, opt_attributes, ...var_args);
// Content manipulation
goog.dom.setTextContent(node, text);
goog.dom.appendChild(parent, child);
goog.dom.removeNode(node);Extensive string utilities for testing, transformation, case conversion, manipulation, comparison, and escaping operations.
// String testing
goog.string.isEmpty(str);
goog.string.isEmptyOrWhitespace(str);
goog.string.startsWith(str, prefix);
goog.string.endsWith(str, suffix);
// String transformation
goog.string.trim(str);
goog.string.toCamelCase(str);
goog.string.htmlEscape(str);
goog.string.repeat(string, length);Comprehensive event handling system with listening, dispatching, and management capabilities for both DOM and custom events.
// Event listening
goog.events.listen(src, type, listener, opt_options, opt_handler);
goog.events.listenOnce(src, type, listener, opt_options, opt_handler);
goog.events.unlisten(src, type, listener, opt_options, opt_handler);
// Event dispatching
goog.events.dispatchEvent(src, e);
// Event classes
goog.events.Event;
goog.events.BrowserEvent;
goog.events.EventTarget;Runtime assertion system with type checking, existence verification, and debugging utilities for development and testing.
// Basic assertions
goog.asserts.assert(condition, opt_message, ...var_args);
goog.asserts.fail(opt_message, ...var_args);
// Type assertions
goog.asserts.assertNumber(value, opt_message, ...var_args);
goog.asserts.assertString(value, opt_message, ...var_args);
goog.asserts.assertFunction(value, opt_message, ...var_args);
goog.asserts.assertArray(value, opt_message, ...var_args);Mathematical utilities including basic functions, coordinate systems, geometric calculations, and range operations.
// Basic math
goog.math.clamp(value, min, max);
goog.math.lerp(a, b, x);
goog.math.randomInt(a);
// Coordinate classes
goog.math.Coordinate;
goog.math.Size;
goog.math.Rect;
goog.math.Box;Object manipulation utilities including iteration, property management, comparison, cloning, and extension operations.
// Object iteration
goog.object.forEach(obj, f, opt_obj);
goog.object.map(obj, f, opt_obj);
goog.object.filter(obj, f, opt_obj);
// Object manipulation
goog.object.getKeys(obj);
goog.object.getValues(obj);
goog.object.clone(obj);
goog.object.extend(target, ...var_args);/**
* Global goog namespace
*/
interface goog {
// Core module system
provide(name: string): void;
require(name: string): void;
// Global constants
DEBUG: boolean;
LOCALE: string;
// Type checking functions
isDefAndNotNull(val: any): boolean;
isFunction(val: any): boolean;
isObject(val: any): boolean;
isArray(val: any): boolean;
isString(val: any): boolean;
isNumber(val: any): boolean;
isBoolean(val: any): boolean;
}