From 4cc32db51b871d13588b1900b09c7751b410d7fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20Po=C5=82a=C4=87?= Date: Wed, 11 May 2016 22:44:33 +0200 Subject: [PATCH] Replace unique with unique_by in documentation --- README.md | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 586d507..31adb5a 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Listing names of all authors ordered by age: ```twig -{% for author in articles|map(=> _.author)|unique|sort_by(=> _.age) %} +{% for author in articles|map(=> _.author)|unique_by('===')|sort_by(=> _.age) %} * {{ author.name }}, {{ author.age }} {% endfor %} ``` @@ -111,11 +111,29 @@ Returns array of elements that passes a test specified by lambda. ---------------------------------------------------------------- - -### |unique -**Signature:** `array|unique` + +### |unique_by +**Signature:** `array|unique_by(lambda|'==='|'==')` + +Returns array of unique elements. Uniqueness is checked with +passed lambda. PHP operators == or === will be used if +string '==' or '===' is passed instead of lambda. + +Lambda should have two arguments - items to check. Keys of +elements are also passed as third and fourth argument. + +```twig + {% for i in [1, 2, 2, 3, 1]|unique_by((i1;i2) => i1 == i2) %} + {{ i }} {# prints '1 2 3' #} + {% endfor %} +``` +equivalent +```twig + {% for i in [1, 2, 2, 3, 1]|unique_by('==') %} + {{ i }} {# prints '1 2 3' #} + {% endfor %} +``` -Returns array with every element occurring only once. ----------------------------------------------------------------