DEV Community

Zako Mako
Zako Mako

Posted on

New library for Faceone

You can now import a file called PearLIXIn your code in Faceone to import a library. This runs on apps script JavaScript and it’s made with AI. Here is the code for installation:


/**
 * PEARLIX - Universal Utility Library
 * Works in: Google Apps Script (GAS), Node.js, and Browser JS
 * @license MIT
 */
(function(global) {
  'use strict';

  // ======================
  // 1. ENVIRONMENT DETECTION
  // ======================
  const isGAS = typeof ScriptApp !== 'undefined';
  const isNode = typeof process !== 'undefined' && process.versions && process.versions.node;
  const isBrowser = typeof window !== 'undefined';

  // ======================
  // 2. CROSS-PLATFORM UTILITIES
  // ======================
  const Utils = {
    // Date formatting (works everywhere)
    formatDate: function(date, format = 'yyyy-MM-dd', timezone) {
      if (isGAS) {
        return Utilities.formatDate(date, timezone || Session.getScriptTimeZone(), format);
      } else {
        // Fallback for Node.js/Browser
        const pad = num => num.toString().padStart(2, '0');
        return format
          .replace('yyyy', date.getFullYear())
          .replace('MM', pad(date.getMonth() + 1))
          .replace('dd', pad(date.getDate()));
      }
    },

    // UUID generation (platform-specific)
    generateUUID: function() {
      if (isGAS) return Utilities.getUuid();
      if (isNode) return require('crypto').randomUUID();
      if (isBrowser) return window.crypto.randomUUID();
      return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        const r = Math.random() * 16 | 0;
        return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
      });
    },

    // Sleep/delay (platform-specific)
    sleep: function(ms) {
      if (isGAS) Utilities.sleep(ms);
      else if (isNode) require('util').promisify(setTimeout)(ms);
      else if (isBrowser) return new Promise(resolve => setTimeout(resolve, ms));
    },

    // Safe JSON parsing (universal)
    safeParseJSON: function(jsonString) {
      try {
        return JSON.parse(jsonString);
      } catch (e) {
        this.log('JSON Error: ' + e.message);
        return null;
      }
    },

    // Logging (adapts to environment)
    log: function(message) {
      if (isGAS) console.log('[PearLIX-GAS] ' + message);
      else if (isNode) console.log('[PearLIX-Node] ' + message);
      else if (isBrowser) console.log('[PearLIX-Browser] ' + message);
    }
  };

  // ======================
  // 3. EXPORT FOR DIFFERENT ENVIRONMENTS
  // ======================
  if (typeof module !== 'undefined' && module.exports) {
    // Node.js/CommonJS
    module.exports = Utils;
  } else if (typeof define === 'function' && define.amd) {
    // AMD/RequireJS
    define([], () => Utils);
  } else {
    // Browser/Google Apps Script
    global.PearLIX = Utils;
  }
})(this || window || global);

function testPearLIX_GAS() {
  PearLIX.log("Running in GAS!");
  const today = PearLIX.formatDate(new Date());
  Logger.log(today); // "2023-11-15"
}

Enter fullscreen mode Exit fullscreen mode

There is no compatibility with FaceoneScript we are adding that soon. If it doesn’t work then we will make improvements

Thank you!

Top comments (0)