CRM_Utils_JS
in package
Parse Javascript content and extract translatable strings.
Tags
Table of Contents
Methods
- convertSingleQuoteString() : string|null
- decode() : mixed
- Decodes a js variable (not necessarily strict json but valid js) into a php variable.
- dedupeClosures() : array<string|int, string>
- Identify duplicate, adjacent, identical closures and consolidate them.
- encode() : string
- Encodes a variable to js notation (not strict json) suitable for e.g. an angular attribute.
- getRawProps() : array<string|int, mixed>
- Gets the properties of a javascript object/array WITHOUT decoding them.
- parseStrings() : array<string|int, mixed>
- Parse a javascript file for translatable strings.
- stripComments() : string
- This is a primitive comment stripper. It doesn't catch all comments and falls short of minification, but it doesn't munge Angular injections and is fast enough to run synchronously (without caching).
- writeObject() : string
- Converts a php array to javascript object/array notation (not strict JSON).
Methods
convertSingleQuoteString()
public
static convertSingleQuoteString(string $str, bool $throwException) : string|null
Parameters
- $str : string
- $throwException : bool
Tags
Return values
string|nulldecode()
Decodes a js variable (not necessarily strict json but valid js) into a php variable.
public
static decode(string $js[, bool $throwException = FALSE ]) : mixed
This is similar to using json_decode($js, TRUE) but more forgiving about syntax.
ex. {a: 'Apple', 'b': "Banana", c: [1, 2, 3]} Returns: [ 'a' => 'Apple', 'b' => 'Banana', 'c' => [1, 2, 3], ]
Parameters
- $js : string
- $throwException : bool = FALSE
Tags
dedupeClosures()
Identify duplicate, adjacent, identical closures and consolidate them.
public
static dedupeClosures(array<string|int, mixed> $scripts, array<string|int, mixed> $localVars, array<string|int, mixed> $inputVals) : array<string|int, string>
Note that you can only dedupe closures if they are directly adjacent and have exactly the same parameters.
Also dedupes the "use strict" directive as it is only meaningful at the beginning of a closure.
Parameters
- $scripts : array<string|int, mixed>
-
Javascript source.
- $localVars : array<string|int, mixed>
-
Ordered list of JS vars to identify the start of a closure.
- $inputVals : array<string|int, mixed>
-
Ordered list of input values passed into the closure.
Return values
array<string|int, string> —Javascript source.
encode()
Encodes a variable to js notation (not strict json) suitable for e.g. an angular attribute.
public
static encode(mixed $value) : string
Like json_encode() but the output looks more like native javascript, with single quotes around strings and no unnecessary quotes around object keys.
Ex input: [ 'a' => 'Apple', 'b' => 'Banana', 'c' => [1, 2, 3], ] Ex output: {a: 'Apple', b: 'Banana', c: [1, 2, 3]}
Parameters
- $value : mixed
Return values
stringgetRawProps()
Gets the properties of a javascript object/array WITHOUT decoding them.
public
static getRawProps(string $js) : array<string|int, mixed>
Useful when the object might contain js functions, expressions, etc. which cannot be decoded. Returns an array with keys as property names and values as raw strings of js.
Ex Input: {foo: getFoo(arg), 'bar': function() {return "bar";}} Returns: [ 'foo' => 'getFoo(arg)', 'bar' => 'function() {return "bar";}', ]
Parameters
- $js : string
Tags
Return values
array<string|int, mixed>parseStrings()
Parse a javascript file for translatable strings.
public
static parseStrings(string $jsCode) : array<string|int, mixed>
Parameters
- $jsCode : string
-
Raw Javascript code.
Return values
array<string|int, mixed> —Array of translatable strings
stripComments()
This is a primitive comment stripper. It doesn't catch all comments and falls short of minification, but it doesn't munge Angular injections and is fast enough to run synchronously (without caching).
public
static stripComments(string $script) : string
At time of writing, running this against the Angular modules, this impl of stripComments currently adds 10-20ms and cuts ~7%.
Please be extremely cautious about extending this. If you want better minification, you should probably remove this implementation, import a proper JSMin implementation, and cache its output.
Parameters
- $script : string
Return values
stringwriteObject()
Converts a php array to javascript object/array notation (not strict JSON).
public
static writeObject(array<string|int, mixed> $obj[, bool $encodeValues = FALSE ]) : string
Does not encode keys unless they contain special characters. Does not encode values by default, so either specify $encodeValues = TRUE, or pass strings of valid js/json as values (per output from getRawProps).
Parameters
- $obj : array<string|int, mixed>
- $encodeValues : bool = FALSE