Skip to content

Commit

Permalink
Add TOC for builtin methods page
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Nov 8, 2023
1 parent 498f12d commit 6cf0e31
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 36 deletions.
4 changes: 2 additions & 2 deletions docs/articles/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ $example: => [$, $$];
// Result: [1, 2]
```

The following example demonstrates how to sum up an array using [`reduce()`](./methods-builtin.md#reducefn-initvalue) method and a function, where `$` is an array element and `$$` is an accumulator value:
The following example demonstrates how to sum up an array using [`reduce()`](./methods-builtin.md#reduce) method and a function, where `$` is an array element and `$$` is an accumulator value:

```jora
[1, 2, 3, 4].reduce(=> $$ + $, 0)
Expand Down Expand Up @@ -80,7 +80,7 @@ foo asc, bar.size() desc
// 0
```

These functions are useful for built-in methods like [`sort()`](./sort.md), [`min()`](./methods-builtin.md#mincompare), [`max()`](./methods-builtin.md#maxcompare) and others.
These functions are useful for built-in methods like [`sort()`](./sort.md), [`min()`](./methods-builtin.md#min), [`max()`](./methods-builtin.md#max) and others.

```jora
$input: [{ foo: 3 }, { foo: 1 }, { foo: 5 }];
Expand Down
50 changes: 25 additions & 25 deletions docs/articles/methods-builtin.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Built-in methods

## avg(getter)
## avg(<!--getter-->)

The `avg(getter)` method calculates the [arithmetic mean](https://en.wikipedia.org/wiki/Arithmetic_mean), also known as the average, of a collection of numbers. The arithmetic mean is computed by adding all the numbers in the collection and then dividing by the total count of numbers. This method is equivalent to the expressions `numbers() | sum() / size()` or `sum() / count()`.

Expand Down Expand Up @@ -42,7 +42,7 @@ Similar to `Boolean()` in JavaScript, but treats *empty arrays* and *objects wit
// Result: true
```

## count(getter)
## count(<!--getter-->)

The `count()` method calculates the number of non-undefined values present in the input array. It processes each value in the array through a `getter` function (default function is `=> $`). If the processed value is not `undefined`, it increments the count by 1. If the input is not an array, the method returns `0`. This method is functionally equivalent to the expression `numbers().size()`.

Expand Down Expand Up @@ -89,7 +89,7 @@ Similar to `Object.entries()` in JavaScript, using `{ key, value }` objects for
// Result: []
```

## filter(fn)
## filter(<!--fn-->)

The same as `Array#filter()` in JavaScript, `filter(fn)` is equivalent to `.[fn()]` (see [Filtering](./filter.md)).

Expand All @@ -112,11 +112,11 @@ Similar to `Object.fromEntries()` in JavaScript, expects array `{ key, value }`
// Result: { a: 42, b: 123 }
```

## group(fn, fn)
## group(<!--fn, fn-->)

Group array items by a value fetched with the first getter and return an array of `{ key, value }` entries (see [Grouping](./group.md)).

## indexOf(value, fromIndex)
## indexOf(<!--value, fromIndex-->)

Returns the first index of the specified value, starting the search at `fromIndex`. If `fromIndex` is not provided or cannot be converted to a number, the search starts from index `0`. The method returns `-1` if the value is not found or if the input doesn't implement the `indexOf()` method. Unlike JavaScript, this method supports index searching for `NaN` values.

Expand All @@ -137,7 +137,7 @@ Returns the first index of the specified value, starting the search at `fromInde
// Result: 1
```

## join(separator)
## join(<!--separator-->)

The same as `Array#join()` in JavaScript. When `separator` is not specified, `","` is used.

Expand Down Expand Up @@ -167,7 +167,7 @@ The same as `Object.keys()` in JavaScript.
// Result: []
```

## lastIndexOf(value, fromIndex)
## lastIndexOf(<!--value, fromIndex-->)

Returns the first index of the specified value starting from the end at `fromIndex`. If `fromIndex` is not specified or cannot be converted to a number, it defaults to array or string length. The method returns `-1` if the value is not found or if the input doesn't implement the `lastIndexOf()` method. Unlike JavaScript, this method supports index searching for `NaN` values.

Expand All @@ -188,7 +188,7 @@ Returns the first index of the specified value starting from the end at `fromInd
// Result: 3
```

## map(fn)
## map(<!--fn-->)

The same as `Array#map()` in JavaScript, is equivalent to `.(fn())` (see [Mapping](./map.md)).

Expand All @@ -202,7 +202,7 @@ $getA: => a;
// Result: [1, 2]
```

## match(pattern, matchAll)
## match(<!--pattern, matchAll-->)

Similar to `String#match()`. `pattern` might be a RegExp or string. When `matchAll` is truthy, returns an array of all occurrences of the `pattern`. Expressions `match(/…/g)` and `match(/…/, true)` are equivalent.

Expand Down Expand Up @@ -259,7 +259,7 @@ Similar to `String#match()`. `pattern` might be a RegExp or string. When `matchA
// }]
```

## max(compare)
## max(<!--compare-->)

The `max(compare)` method returns the maximum value from an array or a string, excluding `undefined`. It uses natural comparison for string values by default. When applied to an array, the method returns `undefined` if the array is empty or if a comparator function returns `0` for all values when compared with `undefined`.

Expand Down Expand Up @@ -293,7 +293,7 @@ $input.max(a desc)
// Result: 'w'
```

## median(getter)
## median(<!--getter-->)

Computes the [median](https://en.wikipedia.org/wiki/Median) (the second [quartile](https://en.wikipedia.org/wiki/Quartile)) of the values in an array. It's a shortcut for `percentile(50)` or `p(50)` (see [percentile()](#percentilevalue-getter)).

Expand All @@ -306,7 +306,7 @@ Computes the [median](https://en.wikipedia.org/wiki/Median) (the second [quartil
// Result: 3.5
```

## min(compare)
## min(<!--compare-->)

The `min()` method returns the minimum value from an array or a string. It uses natural comparison for string values by default. When applied to an array, the method returns `undefined` if the array is empty or if a comparator function returns `0` for all values when compared with `undefined`.

Expand Down Expand Up @@ -335,7 +335,7 @@ $input.min(a desc)
// Result: ' '
```

## numbers(getter)
## numbers(<!--getter-->)

The `numbers()` method returns an array of numbers derived from the input values. It ignores `undefined` values returned by the `getter` function (the default `getter` function is `=> $`, which returns the value itself). All other values are converted to numbers including `NaN` and `Infinity`. When converting a value to a number, any objects and arrays are converted to `NaN`.

Expand All @@ -356,11 +356,11 @@ The `numbers()` method is utilized internally by statistical methods such as `su

> Note: When applying a statistical computation to an array of objects, it is recommended to use a custom `getter` with the method rather than using dot notation or mapping. This is because dot notation and mapping ignore duplicate values. For instance, the query `[…].age.numbers()` might return `[10, 20]` for the last example instead of the expected `[10, 20, 10]`, which would be correctly returned by the query `[…].numbers(=> age)`.
## p(k, getter)
## p(<!--k, getter-->)

Alias for [`percentile()`](#percentilek-getter) method.

## percentile(k, getter)
## percentile(<!--k, getter-->)

This function computes the [percentile](https://en.wikipedia.org/wiki/Percentile) of values in an array. It returns `undefined` if the input is an empty array, not an array, or if the `k` parameter is not specified or falls outside the range of `[0..100]`. The function utilizes the same numbers as the [`numbers()`](#numbersgetter) method, given the same `getter` parameter. If the input (after processing through `getter`) contains a `NaN`, the function will always return `NaN`.

Expand Down Expand Up @@ -401,7 +401,7 @@ Get a value by a key, index, or function. The method repeats behaviour of [Brack
// Result: 2
```

## reduce(fn, initValue)
## reduce(<!--fn, initValue-->)

The same as `Array#reduce()` in JS. Use `$$` to access the accumulator and `$` for the current value, e.g., find the max value:

Expand All @@ -410,7 +410,7 @@ The same as `Array#reduce()` in JS. Use `$$` to access the accumulator and `$` f
// Result: 5
```

## replace(pattern, replacement)
## replace(<!--pattern, replacement-->)

The same as `String#replaceAll()` in JavaScript, but also works for arrays. When `pattern` is RegExp, a `g` flags adds automatically if omitted. When applying to arrays it's similar to `.($ = pattern ? replacement : $)`, but without dropping duplicate values and inlining arrays.

Expand Down Expand Up @@ -472,19 +472,19 @@ Returns count of entries in an object, otherwise returns `length` property value
// Result: 0
```

## slice(from, to)
## slice(<!--from, to-->)

The same as `Array#slice()` and `String#slice()` in JavaScript (see also [Slice notation](./slice-notation.md)).

## sort(compare)
## sort(<!--compare-->)

Sort an array by a value fetched with getter (`fn`). Can use sorting function definition syntax with `asc` and `desc` (see [Sorting](./sort.md))

## split(pattern)
## split(<!--pattern-->)

The same as `String#split()` in JavaScript. `pattern` may be a string or regex.

## stdev(getter)
## stdev(<!--getter-->)

Returns the [standard deviation](https://en.wikipedia.org/wiki/Standard_deviation) (`𝜎`) of a population, is the square root of the variance.

Expand All @@ -497,7 +497,7 @@ Returns the [standard deviation](https://en.wikipedia.org/wiki/Standard_deviatio
// Result: 1
```

## sum(getter)
## sum(<!--getter-->)

Computes the sum of the values in an array. It returns `undefined` for non-array values and empty arrays. The method uses the same numbers as [`numbers()`](#numbersgetter) method with the same `getter` parameter returns. The method employs the [Kahan–Babuška summation algorithm](https://en.wikipedia.org/wiki/Kahan_summation_algorithm) to minimize numerical errors in the result.

Expand Down Expand Up @@ -546,7 +546,7 @@ Since arrays are always converting to `NaN`. To summing array of arrays, a summa
// Result: 7
```

## toLowerCase(locales)
## toLowerCase(<!--locales-->)

The same as `String#toLocaleLowerCase()` in JavaScript.

Expand All @@ -555,7 +555,7 @@ The same as `String#toLocaleLowerCase()` in JavaScript.
// Result: "hello world!"
```

## toUpperCase(locales)
## toUpperCase(<!--locales-->)

The same as `String#toLocaleUpperCase()` in JavaScript.

Expand All @@ -577,7 +577,7 @@ The same as `String#trim()` in JavaScript.

The same as `Object.values()` in JavaScript.

## variance(getter)
## variance(<!--getter-->)

Returns the [variance](http://en.wikipedia.org/wiki/Variance) (`𝜎²`) of a [population](https://en.wikipedia.org/wiki/Variance#Population_variance) (the squared deviation from the mean).

Expand Down
2 changes: 1 addition & 1 deletion docs/discovery/common.css
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
.view-struct_code-postlude {
padding-top: 1px;
padding-bottom: 1px;
padding-left: 29px;
padding-left: 30px;
}
.view-struct_code-postlude:not(.struct-expanded-value, :hover) {
background-color: transparent;
Expand Down
12 changes: 11 additions & 1 deletion docs/discovery/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ const assert = require('assert');
const marked = require('marked');
const jora = require('jora');
const { parseExample } = require('./parse-example.js');
const mathMethods = new Set([
'abs', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh',
'cbrt', 'ceil', 'clz32', 'cos', 'cosh', 'exp', 'expm1', 'floor',
'fround', 'hypot', 'imul', 'ln', 'log10', 'ln1p', 'log2', 'pow',
'round', 'sign', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc'
]);

function md(filename) {
return fs.readFileSync(path.join(__dirname, filename) + '.md', 'utf8');
Expand Down Expand Up @@ -152,7 +158,11 @@ function processChangelog(changelog, methods) {

module.exports = function() {
const examples = [];
const methods = Object.keys(jora.methods).sort().map(name => ({ name, examples: [] }));
const methods = Object.keys(jora.methods).sort().map(name => ({
name,
namespace: mathMethods.has(name) ? 'math' : undefined,
examples: []
}));
const changelog = { slug: 'changelog', title: 'Changelog', headers: true, content: md('../../CHANGELOG') };
const articles = [
{ slug: 'getting-started', title: 'Getting started', expanded: true, headers: true },
Expand Down
65 changes: 65 additions & 0 deletions docs/discovery/pages/article.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,68 @@
.page-article .prev-next-nav {
margin-top: 2em;
}

.page-article .view-page-header__content {
display: flex;
align-items: center;
gap: 1ex;
}
.page-article .scroll-to-top {
margin-top: 5px;
transition: .25s ease-in;
transition-property: opacity, visibility;
opacity: 0;
visibility: hidden;
}
.page-article.page_overscrolled .scroll-to-top {
opacity: 1;
visibility: visible;
}
.page-article .scroll-to-top-button {
font-size: 12px;
padding: 4px 8px;
}

.page-article .toc-section-header {
text-transform: uppercase;
font-size: 11px;
opacity: .85;
margin: 8px 0 0 0;
}
.page-article .toc-group-item {
white-space: nowrap;
margin-right: 1ex;
}
.page-article .toc-litera {
display: inline-block;
color: #888;
text-transform: uppercase;
margin-right: 1ex;
}
.page-article .toc-group {
display: inline-flex;
gap: .75ex;
}

.page-article .toc-filter {
margin-top: -20px;
margin-left: -10px;
margin-right: -10px;
max-width: 870px;
}
.page-article .toc-filter > .view-input {
margin-bottom: .5em;
}
.page-article .toc-filter > .content .view-list {
margin-left: 10px;
}
.page-article .toc-filter > .content .view-list::before {
padding: 0;
}

.page-article .toc-filter .view-text-match {
background: #68624685;
text-decoration-color: #8b7b42;
border: none;
color: #b6aa6a;
}
Loading

0 comments on commit 6cf0e31

Please sign in to comment.