New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

client-side-common-utils

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

client-side-common-utils

A collection of classes used for client-side development.

  • 1.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source

Warning

This package is deprecated. Please use simple-common-utils instead.

This is a collection of classes used for JS client-side development.

  1. Usage
  2. Version history

Usage

  1. ArrayStringifier
  2. DottedStringObject
  3. StaticUtils
  4. utf8
ArrayStringifier

Given an array, produces its string representation.

import { ArrayStringifier } from "client-side-common-utils"; 
  • constructor()

    Constructs a class instance.

     new ArrayStringifier(array);
    

    gives exactly the same result as

     new ArrayStringifier()
         .setArray(array)
         .setSeparator(", ");
    
  • process() / toString()

    Returns a string representation of the array.

The following methods return this for method chaining.

  • setPrefix()

    Sets a prefix to be added to the resulting string.

     arrayStringifier.setPrefix(
         prefix, // String.
         addIfArrayLength // Boolean. If true the prefix will be added ONLY if the array is not empty. Default: true.
     );
    
  • setArray()

    Sets an array to stringify.

     arrayStringifier.setArray(array);
    
  • setSeparator()

    Sets a separator to be used.

     arrayStringifier.setSeparator(
         separator // String
     );
    
  • setElementProcessor()

    Sets an element processor for fine-grained per-element processing. An element processor is a function the return value of which will be used to determine the value and optionally the separator for the current array element. If an element processor returns an object with methods getElement() and getSeparator() they will be used to get the value and the separator for the current array element. If the method getElement() is not present in the return value, the latter will be used as the value for the current array element as is and the separator set by setSeparator() will be used.

     const arrayStringifier = new ArrayStringifier();
     
     arrayStringifier
         .setArray([1, 2, 3, 4])
         .setElementProcessor(element => element % 2 ? "Something odd" : element)
         .process(); // Something odd, 2, Something odd, 4
     
     arrayStringifier
         .setArray([1, 2, 3, 4])
         .setElementProcessor(element => !(element % 2) ? element : element == 1 ? {
             getElement() {
                 return "";
             },
             getSeparator() {
                 return "";
             }
         } : "Something odd")
         .process(); // 2, Something odd, 4
    
  • setPostfix()

    Sets a postfix to be added to the resulting string.

     arrayStringifier.setPostfix(
         postfix, // String.
         addIfArrayLength // Boolean. If true the postfix will be added ONLY if the array is not empty. Default: true.
     );
    
DottedStringObject

Provides a way to get and set objects properties with dot separated strings. All methods are static.

import { DottedStringObject } from "client-side-common-utils";
  • getProperty()

    Gets a property value.

     DottedStringObject.getProperty(
         object, // Object.
         fullPropertyName, // String. A dot separated full property name.
         defaultValue // Object. This value is returned if the property doesn't exist.
     );
    

    Example:

     const const obj = {
         f1: 10,
         obj1: {
             f1: 20
         }
     };
     
     DottedStringObject.getProperty(obj, "f1"); // 10
     DottedStringObject.getProperty(obj, "f1", "aaa"); // 10
     DottedStringObject.getProperty(obj, "obj1.f1"); // 20
     DottedStringObject.getProperty(obj, "obj1.f2", "default"); // default
    
  • setProperty()

    Sets a property value.

     DottedStringObject.setProperty(
         object, // Object.
         fullPropertyName, // String. A dot separated full property name.
         value // Object. A value to set.
     );
    

    Example:

     const obj = {};
     
     DottedStringObject.setProperty(obj, "f1", 10); // { f1: 10 }
     DottedStringObject.setProperty(obj, "obj1", 20); // { f1: 10, obj1: 20 }
     DottedStringObject.setProperty(obj, "obj1", {}); // { f1: 10, obj1: {} }
     DottedStringObject.setProperty(obj, "obj1.f2", 30); // { f1: 10, obj1: { f2: 30 } }
    
StaticUtils

A collection of different utility methods. All the methods in this class are static.

import { StaticUtils } from "client-side-common-utils";
  • round()

    Rounds value to decimals digits after the decimal point. Thanks MarkG!

     StaticUtils.round(value, decimals);
    
     StaticUtils.round(10.2); // 10.2
     StaticUtils.round(10.2, 0); // 10
     StaticUtils.round(10.5, 0); // 11
     StaticUtils.round(10.523, 1); // 10.5
     StaticUtils.round(10.525, 2); // 10.53
    
  • encodedUtf8ToByteArray()

    Converts the passed utf8-encoded string to a byte array.

     import utf8 from "utf8";
     
     StaticUtils.encodedUtf8ToByteArray(
         utf8.encode("abcфыва")); // [ 97, 98, 99, 209, 132, 209, 139, 208, 178, 208, 176 ]
    
  • ensureBounds()

    Ensures min <= value <= max.

     StaticUtils.ensureBounds(value, min, max);
     
     StaticUtils.ensureBounds(10, 2, 18); // 10
     StaticUtils.ensureBounds(100, 2, 18); // 18
     StaticUtils.ensureBounds(100, 200, 1800); // 200
    
  • pushAndReturnElement()

    Pushes element to array and returns element.

     StaticUtils.pushAndReturnElement(array, element);
    
  • quoteIfString()

    Quotes value if it's a string.

     StaticUtils.quoteIfString(10); // 10
     StaticUtils.quoteIfString("10"); // "10"
    
  • safeQuoteIfString()

    Invokes quoteIfString() passing value to it if quoteIfString is true.

     StaticUtils.safeQuoteIfString(value, quoteIfString);
    
  • objectToArray()

    Converts object to an array and returns it. Nested objects are not parsed.

     StaticUtils.objectToArray({a: "10", b: 20}); // [ '10', 20 ]
     StaticUtils.objectToArray({a: "10", b: 20, c: {a: 10}}); //  [ '10', 20, { a: 10 } ]
    

Code is taken from here for the next two methods.

  • escapeRegExp()

    Given a string, escapes all occurences of symbols that have special meaning in regular expressions.

     StaticUtils.escapeRegExp("a"); // a
     StaticUtils.escapeRegExp("*a^"); // \*a\^
    
  • replaceAll()

    Implements a "replace all" functionality for a string.

     StaticUtils.replaceAll("abc", "b", "10"); // a10c
     StaticUtils.replaceAll("a^b*c", "^b", "10"); // a10*c
    
utf8

Version history

Version numberChanges
v1.0.21. Readme updated.
2. StaticUtils.round(): if decimals isn't passed, value is returned.
3. StaticUtils.ensureBounds(): if min exceeds max an exception is thrown.
v1.0.11. Readme updated.
2. ArrayStringifier.toString() is added. It's identical to ArrayStringifier.process().
v1.0.0Initial release.


Written with StackEdit.

Keywords

FAQs

Package last updated on 05 Sep 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc