Menu Icon

Built-in Functions

This article shows how to use Peaka functions in JEXL

stringConcat(value)

It makes your value to string.

stringConcat(123456);
// => '123456'

convertLocaleString(value, locale, options)

It makes your value to string with your locale.

  • value can be number or date type and it's requeired.
  • locale is your locale language like 'fr' for French or 'tr' for Turkish and it's requeired.
  • options is an object and you can configure like below and it's not requeired.
{
  weekday: "long",
  year: "numeric",
  month: "long",
  day: "numeric",
};
convertLocaleString(123456.789, "de", {
  style: "currency",
  currency: "EUR",
});

// => 123.456,79 €

convertLocaleDateString(value, locale, options)

It makes your value to string with your locale.

  • value can be string or date type and it's requeired.
  • locale is your locale language like 'fr' for French or 'tr' for Turkish and it must be string and it's requeired.
  • options is an object and you can configure like below and it's not requeired.
{
  weekday: "long",
  year: "numeric",
  month: "long",
  day: "numeric",
};
convertLocaleDateString("Tue Aug 15 2023 10:23:41", "de", {
  weekday: "long",
  year: "numeric",
  month: "long",
  day: "numeric",
});

// => Dienstag, 15. August 2023

push(arr, value)

It adds a new value to the array.

  • arr must be array type and it's required.
  • value can be any type and it's required
push([1, 2, 3, 4, 5], 6);

// => [1, 2, 3, 4, 5, 6]

addKey(object, key, value)

It adds a new key and value to your object

  • object must be object type and it's required.
  • key must be string type and it's required.
  • type of value may be string, number, date, object, array... and it's required.
addKey(
  {
    weekday: "long",
    year: "numeric",
    month: "long",
  },
  "day",
  "numberic"
);

/* =>
  {
    weekday: "long",
    year: "numeric",
    month: "long",
    day: "numeric",
  }
*/

removeKey(object, key)

It deletes a key of your object.

  • object must be object type and it's required.
  • key must be string type and it's required.
removeKey(
  {
    weekday: "long",
    year: "numeric",
    month: "long",
  },
  "month"
);

/* =>
  {
    weekday: "long",
    year: "numeric",
  }
*/

mapToKey(objects, key)

It brings value of key that you want.

  • objects is an array includes objects and it's required.
  • key must be string type and it's required.
mapToKey([{ value: 11 }, { value: 22 }], "value");

// => [11,22]

filterByKeys(objects, keys)

It filters by your keys.

  • objects is an array includes objects and it's required.
  • key must be array includes string and it's required.
filterByKeys(
  [
    { name: "Joe", surname: "Doe" },
    { name: "Can", surname: "Tuna" },
  ],
  ["surname"]
);

// => [{surname:'Doe'},{surname: 'Tuna'}]

fromEntries(object)

It returns your object keys and values in arrays.

  • object must be objects type and it's required.
fromEntries({
  a: "somestring",
  b: 42,
});

// => [['a', 'something'], ['b', 42]]

fromKeysAndValues(keys, values)

It creates an object with your keys and values. You need to enter them by your order.

fromKeysAndValues(["one", "two", "three"], [1, 2, 3]);

// => { one: 1, two: 2, three: 3}

uuid()

It return an unique id.

uuid();

// => 4d85bbb9-f602-40f9-bd07-48eda130a1b5

len(arr)

It return length of array.

  • arr is an array and it's required.
len([1, 2, 3, 4, 5, 6]);

// => 6

updateKey(object, oldKey, newKey)

It updates a key that you want. It find your old key by oldKey and replace with newKey.

  • object must be object type and it's required.
  • oldKey must be string type and it's required.
  • newKey must be string type and it's required.
updateKey(
  {
    a: "somestring",
    b: 42,
  },
  "b",
  "newKeyC"
);

/* => 
{ 
  a: "somestring",
  newKeyC: 42
}
*/

findDeep(arr,recursiveKey, searchKey, value)

It finds your value by searchKey and returns value of it. If it doesn't find it or value of searchKey doesn't equal to value, it returns 'undefined'.

  • arr may be object or array type and it's required.
  • recursiveKey is key of recurse the array. If you don't have make it empty string('').
  • serachKey is the key which of you're looking for value.
  • value is to compare value of searchKey.
findDeep(
  {
    item: "string",
    children: [
      {
        item: "string2",
        children: [{ valueKey: "myValue" }],
      },
    ],
  },
  "children",
  "valueKey",
  "myValue"
);

// => {valueKey: "myValue"}

removeIndex(array, index)

It removes the element of given index.

  • array must be an array and it's required.
  • index must be number of string and it's required.
removeIndex(["one", "two", "three"], 0);

// => ['two'. 'three']

jsonStringify(value)

  • It makes string your value.

  • value may be any type and it's required.

jsonStringify({ name: "John", age: 30, city: "New York" });

// => "{ name: "John", age: 30, city: "New York" }"

jsonParse(value)

  • It parses your value.

  • value may be any type and it's required.

jsonStringify("{ name: 'John', age: 30, city: 'New York' }");

// => { name: "John", age: 30, city: "New York" }

withQuotes(value)

It adds withQuotes from start and end of the string.

  • value must be string and it's required.
withQuotes("can tuna");
// => "can tuna"

trimQuotes(value)

It removes backslash from start and end of the string.

  • value must be string and it's required.
trimQuotes("\"How to add doublequotes\"");
// => can tuna

fetch(url,options)

It sends a request.

  • url is your request url and it's required.
  • options is your request options and it's required.
fetch("https://jsonplaceholder.typicode.com/posts", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
  },
});

getQueryParameter(url, key)

It returns an query parameter.

  • url is your url and it must be string and required.
  • key is the key of your parameter and it must be string and required.
getQueryParameter("https://example.com?foo=1&bar=2", "foo");

// => 1

getQueryParameters()

It returns all query parameters.

  • url is your url and it's required.
  • key is the key of your parameters and it's required.
getQueryParameter("https://example.com?foo=1&foo=2", "foo");

//  => [1, 2]

now()

It returns date of now.

now();

// => 2023-08-15T12:53:50.862Z