├── logo.png ├── languageFix.dart ├── .gitattributes ├── snippets ├── sum.md ├── max.md ├── min.md ├── isDivisible.md ├── distinct.md ├── drop.md ├── radsToDegrees.md ├── degreesToRads.md ├── head.md ├── isEven.md ├── isOdd.md ├── fibonacciNumber.md ├── allEqual.md ├── average.md ├── sample.md ├── dropRight.md ├── mapNumRange.md ├── isLowerCase.md ├── isUpperCase.md ├── last.md ├── removeNonASCII.md ├── splitLines.md ├── all.md ├── compact.md ├── none.md ├── reverseString.md ├── nth.md ├── some.md ├── flatten.md ├── filterUnique.md ├── tail.md ├── union.md ├── filterNonUnique.md ├── initial.md ├── everyNth.md ├── digitize.md ├── includesAny.md ├── sumBy.md ├── factorial.md ├── includesAll.md ├── compactWhitespace.md ├── maxBy.md ├── minBy.md ├── mapList.md ├── truncateString.md ├── mostFrequent.md ├── difference.md ├── averageBy.md ├── intersection.md ├── mapString.md ├── fibonacci.md ├── unzip.md ├── isPalindrome.md ├── frequencies.md ├── dropWhile.md ├── pad.md ├── capitalize.md ├── decapitalize.md ├── dropRightWhile.md ├── chunk.md ├── groupBy.md ├── randomIntInRange.md ├── randomDoubleInRange.md ├── countBy.md ├── haveSameContents.md ├── mask.md ├── randomIntListInRange.md ├── words.md ├── bifurcateBy.md ├── symmetricDifference.md ├── zip.md ├── differenceBy.md ├── intersectionBy.md ├── toKebabCase.md ├── toSnakeCase.md ├── isAnagram.md ├── slice.md ├── unionBy.md ├── symmetricDifferenceBy.md ├── toTitleCase.md └── toCamelCase.md ├── snippet-template.md ├── .github ├── config.yml ├── stale.yml └── lock.yml ├── README.md ├── .gitignore ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md └── LICENSE /logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chalarangelo/30-seconds-of-dart/HEAD/logo.png -------------------------------------------------------------------------------- /languageFix.dart: -------------------------------------------------------------------------------- 1 | main() { 2 | print('This file is here only to tag the repository language. Do not delete, please!'); 3 | } 4 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | src/docs/* linguist-documentation 2 | scripts/* linguist-documentation 3 | gatsby-browser.js linguist-documentation 4 | gatsby-config.js linguist-documentation 5 | gatsby-node.js linguist-documentation 6 | gatsby-ssr.js linguist-documentation 7 | .travis/* linguist-documentation 8 | config.js linguist-documentation 9 | -------------------------------------------------------------------------------- /snippets/sum.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: sum 3 | tags: math,list,beginner 4 | --- 5 | 6 | Returns the sum value of a list of numbers. 7 | 8 | - Use `Iterable.reduce()` to sum all the numbers in a list. 9 | 10 | ```dart 11 | num sum(List nums){ 12 | return nums.reduce((num a, num b) => a + b); 13 | } 14 | ``` 15 | 16 | ```dart 17 | sum([1, 2, 3, 4]); // 10 18 | ``` 19 | -------------------------------------------------------------------------------- /snippets/max.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: max 3 | tags: math,list,beginner 4 | --- 5 | 6 | Returns the maximum value in a list of numbers. 7 | 8 | - Use `Iterable.reduce()` in combination with `max()` to find the maximum value. 9 | 10 | ```dart 11 | import 'dart:math'; 12 | 13 | num max(List nums){ 14 | return nums.reduce(max); 15 | } 16 | ``` 17 | 18 | ```dart 19 | max([4, 6, 1, 2, 5]); // 6 20 | ``` 21 | -------------------------------------------------------------------------------- /snippet-template.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: mySnippet 3 | tags: utility,intermediate 4 | --- 5 | 6 | Explain briefly what the snippet does. 7 | 8 | - Explain briefly how the snippet works. 9 | - Use bullet points for your snippet's explanation. 10 | - Try to explain everything briefly but clearly. 11 | 12 | ```dart 13 | // Your snippet code here 14 | ``` 15 | 16 | ```dart 17 | // Your snippet examples here 18 | ``` 19 | -------------------------------------------------------------------------------- /snippets/min.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: min 3 | tags: math,list,beginner 4 | --- 5 | 6 | Returns the minimum value in a list of numbers. 7 | 8 | - Use `Iterable.reduce()` in combination with `min()` to find the minimum value. 9 | 10 | ```dart 11 | import 'dart:math'; 12 | 13 | num min(List nums){ 14 | return nums.reduce(math.min); 15 | } 16 | ``` 17 | 18 | ```dart 19 | min([4, 6, 1, 2, 5]); // 1 20 | ``` 21 | -------------------------------------------------------------------------------- /snippets/isDivisible.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: isDivisible 3 | tags: math,beginner 4 | --- 5 | 6 | Checks if the first integer argument is divisible by the second one. 7 | 8 | - Use the modulo operator (`%`) to check if the remainder is equal to `0`. 9 | 10 | ```dart 11 | bool isDivisible(int dividend, int divisor) { 12 | return dividend % divisor == 0; 13 | } 14 | ``` 15 | 16 | ```dart 17 | isDivisible(6, 3); // true 18 | ``` 19 | -------------------------------------------------------------------------------- /snippets/distinct.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: distinct 3 | tags: list,beginner 4 | --- 5 | 6 | Returns the distinct values in a list. 7 | 8 | - Use `List.toSet()` to get the distinct values in the list, `Set.toList()` to return them as a list. 9 | 10 | ```dart 11 | List distinct(List lst) { 12 | return lst.toSet().toList(); 13 | } 14 | ``` 15 | 16 | ```dart 17 | distinct([1, 2, 2, 3, 4, 4, 5]); // [1, 2, 3, 4, 5] 18 | ``` 19 | -------------------------------------------------------------------------------- /snippets/drop.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: drop 3 | tags: list,beginner 4 | --- 5 | 6 | Returns a new list with `n` elements removed from the left. 7 | 8 | - Use `List.sublist()` to remove the specified number of elements from the left. 9 | 10 | ```dart 11 | List drop(List lst, [int n = 1]) { 12 | return lst.sublist(n); 13 | } 14 | ``` 15 | 16 | ```dart 17 | drop([1, 2, 3]); // [2,3] 18 | drop([1, 2, 3], 2); // [3] 19 | ``` 20 | -------------------------------------------------------------------------------- /snippets/radsToDegrees.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: radsToDegrees 3 | tags: math,beginner 4 | --- 5 | 6 | Converts an angle from radians to degrees. 7 | 8 | - Use `pi` and the radian to degree formula to convert the angle from radians to degrees. 9 | 10 | ```dart 11 | import 'dart:math'; 12 | 13 | num radsToDegrees(num rad) { 14 | return (rad * 180.0) / pi; 15 | } 16 | ``` 17 | 18 | ```dart 19 | radsToDegrees(pi / 2); // 90 20 | ``` 21 | -------------------------------------------------------------------------------- /snippets/degreesToRads.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: degreesToRads 3 | tags: math,beginner 4 | --- 5 | 6 | Converts an angle from degrees to radians. 7 | 8 | - Use `pi` and the degree to radian formula to convert the angle from degrees to radians. 9 | 10 | ```dart 11 | import 'dart:math'; 12 | 13 | num degreesToRads(num deg) { 14 | return (deg * pi) / 180.0; 15 | } 16 | ``` 17 | 18 | ```dart 19 | degreesToRads(90.0); // ~1.5708 20 | ``` 21 | -------------------------------------------------------------------------------- /snippets/head.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: head 3 | tags: list,beginner 4 | --- 5 | 6 | Returns the head of a list. 7 | 8 | - Check if `lst` has a non-zero length, use `lst[0]` if possible to return the first element, otherwise return `null`. 9 | 10 | ```dart 11 | T head(List lst) { 12 | return lst.length > 0 ? lst[0] : null; 13 | } 14 | ``` 15 | 16 | ```dart 17 | head([1, 2, 3]); // 1 18 | head([1]); // 1 19 | head([]); // null 20 | ``` 21 | -------------------------------------------------------------------------------- /snippets/isEven.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: isEven 3 | tags: math,beginner 4 | --- 5 | 6 | Returns `true` if the given number is even, `false` otherwise. 7 | 8 | - Checks whether a number is odd or even using the modulo (`%`) operator. 9 | - Returns `true` if the number is even, `false` if the number is odd. 10 | 11 | ```dart 12 | bool isEven(num n) { 13 | return n % 2 == 0; 14 | } 15 | ``` 16 | 17 | ```dart 18 | isEven(3); // false 19 | ``` 20 | -------------------------------------------------------------------------------- /snippets/isOdd.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: isOdd 3 | tags: math,beginner 4 | --- 5 | 6 | Returns `true` if the given number is odd, `false` otherwise. 7 | 8 | - Checks whether a number is odd or even using the modulo (`%`) operator. 9 | - Returns `true` if the number is odd, `false` if the number is even. 10 | 11 | ```dart 12 | bool isOdd(num n) { 13 | return n % 2 != 0; 14 | } 15 | ``` 16 | 17 | ```dart 18 | isOdd(3); // true 19 | ``` 20 | 21 | -------------------------------------------------------------------------------- /snippets/fibonacciNumber.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: fibonacciNumber 3 | tags: math,recursion,beginner 4 | --- 5 | 6 | Returns the nth term of the Fibonacci sequence. 7 | 8 | - Use recursion to calculate the `n`th term in the Fibonacci sequence. 9 | 10 | ```dart 11 | int fibonacci(int n) { 12 | if (n <= 0) return 0; 13 | return n < 2 ? n : (fibonacci(n - 1) + fibonacci(n - 2)); 14 | } 15 | ``` 16 | 17 | ```dart 18 | fibonacci(6); // 8 19 | ``` 20 | -------------------------------------------------------------------------------- /snippets/allEqual.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: allEqual 3 | tags: list,beginner 4 | --- 5 | 6 | Check if all elements in a list are equal. 7 | 8 | - Use `Iterable.every()` to check if all the elements of the list are the same as the first one. 9 | 10 | ```dart 11 | bool allEqual(List itr) { 12 | return itr.every((i) => i == itr[0]); 13 | } 14 | ``` 15 | 16 | ```dart 17 | allEqual([1, 2, 3, 4, 5, 6]); // false 18 | allEqual([1, 1, 1, 1]); // true 19 | ``` 20 | -------------------------------------------------------------------------------- /snippets/average.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: average 3 | tags: math,list,beginner 4 | --- 5 | 6 | Returns the average value of a list of numbers. 7 | 8 | - Use `Iterable.reduce()` to get the sum of all the numbers in a list, divide by `Iterable.length` to get the average. 9 | 10 | ```dart 11 | num average(List nums){ 12 | return nums.reduce((num a, num b) => a + b) / nums.length; 13 | } 14 | ``` 15 | 16 | ```dart 17 | average([1, 2, 3, 4]); // 2.5 18 | ``` 19 | -------------------------------------------------------------------------------- /snippets/sample.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: sample 3 | tags: list,random,beginner 4 | --- 5 | 6 | Returns a random element from a list. 7 | 8 | - Use `Random.nextInt()` to generate a random integer between `0` and `lst.length` and return the list element at that index. 9 | 10 | ```dart 11 | import 'dart:math'; 12 | 13 | T sample(List lst) { 14 | return lst[Random().nextInt(lst.length)]; 15 | } 16 | ``` 17 | 18 | ```dart 19 | sample([3, 7, 9, 11]); // 9 20 | ``` 21 | -------------------------------------------------------------------------------- /snippets/dropRight.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: dropRight 3 | tags: list,beginner 4 | --- 5 | 6 | Returns a new list with `n` elements removed from the right. 7 | 8 | - Use `List.sublist()` to remove the specified number of elements from the right. 9 | 10 | ```dart 11 | List dropRight(List lst, [int n = 1]) { 12 | return lst.sublist(0, lst.length - n); 13 | } 14 | ``` 15 | 16 | ```dart 17 | dropRight([1, 2, 3]); // [1,2] 18 | dropRight([1, 2, 3], 2); // [1] 19 | ``` 20 | -------------------------------------------------------------------------------- /snippets/mapNumRange.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: mapNumRange 3 | tags: math,beginner 4 | --- 5 | 6 | Maps a number from one range to another range. 7 | 8 | - Returns `n` mapped between `outMin`-`outMax` from `inMin`-`inMax`. 9 | 10 | ```dart 11 | num mapNumRange(num n, num inMin, num inMax, num outMin, num outMax) { 12 | return ((n - inMin) * (outMax - outMin)) / (inMax - inMin) + outMin; 13 | } 14 | ``` 15 | 16 | ```dart 17 | mapNumRange(5, 0, 10, 0, 100); // 50 18 | ``` 19 | -------------------------------------------------------------------------------- /snippets/isLowerCase.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: isLowerCase 3 | tags: string,beginner 4 | --- 5 | 6 | Checks if a string is lower case. 7 | 8 | - Convert the given string to lower case, using `String.toLowerCase()` and compare it to the original. 9 | 10 | ```dart 11 | bool isLowerCase(String str) { 12 | return str == str.toLowerCase(); 13 | } 14 | ``` 15 | 16 | ```dart 17 | isLowerCase('abc'); // true 18 | isLowerCase('a3@$'); // true 19 | isLowerCase('Ab4'); // false 20 | ``` 21 | -------------------------------------------------------------------------------- /snippets/isUpperCase.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: isUpperCase 3 | tags: string,beginner 4 | --- 5 | 6 | Checks if a string is upper case. 7 | 8 | - Convert the given string to upper case, using `String.toUpperCase()` and compare it to the original. 9 | 10 | ```dart 11 | bool isUpperCase(String str) { 12 | return str == str.toUpperCase(); 13 | } 14 | ``` 15 | 16 | ```dart 17 | isUpperCase('ABC'); // true 18 | isUpperCase('A3@$'); // true 19 | isUpperCase('aB4'); // false 20 | ``` 21 | -------------------------------------------------------------------------------- /snippets/last.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: last 3 | tags: list,beginner 4 | --- 5 | 6 | Returns the last element in a list. 7 | 8 | - Check if `lst` has a non-zero length, use `lst[lst.length - 1]` if possible to return the last element, otherwise return `null`. 9 | 10 | ```dart 11 | T head(List lst) { 12 | return lst.length > 0 ? lst[lst.length - 1] : null; 13 | } 14 | ``` 15 | 16 | ```dart 17 | head([1, 2, 3]); // 3 18 | head([1]); // 1 19 | head([]); // null 20 | ``` 21 | -------------------------------------------------------------------------------- /snippets/removeNonASCII.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: removeNonASCII 3 | tags: string,regexp,intermediate 4 | --- 5 | 6 | Removes non-printable ASCII characters. 7 | 8 | - Use `String.replaceAll()` with a regular expression to remove non-printable ASCII characters. 9 | 10 | ```dart 11 | String removeNonASCII(String str) { 12 | return str.replaceAll(RegExp(r'[^\x20-\x7E]'), ''); 13 | } 14 | ``` 15 | 16 | ```dart 17 | removeNonASCII('äÄçÇéÉêlorem-ipsumöÖÐþúÚ'); // 'lorem-ipsum' 18 | ``` 19 | -------------------------------------------------------------------------------- /snippets/splitLines.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: splitLines 3 | tags: string,beginner 4 | --- 5 | 6 | Splits a multiline string into a list of lines. 7 | 8 | - Use `String.split()` and a regular expression to match line breaks and create a list. 9 | 10 | ```dart 11 | List splitLines(String str) { 12 | return str.split(RegExp(r'\r?\n')); 13 | } 14 | ``` 15 | 16 | ```dart 17 | splitLines('This\nis a\nmultiline\nstring.\n'); // ['This', 'is a', 'multiline', 'string.' , ''] 18 | ``` 19 | -------------------------------------------------------------------------------- /snippets/all.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: all 3 | tags: list,beginner 4 | --- 5 | 6 | Returns `true` if the provided predicate function returns `true` for all elements in a collection, `false` otherwise. 7 | 8 | - Use `Iterable.every()` to check if all elements in the collection return `true` based on `fn`. 9 | 10 | ```dart 11 | bool all(Iterable itr, bool Function(T) fn) { 12 | return itr.every(fn); 13 | } 14 | ``` 15 | 16 | ```dart 17 | all([4, 2, 3], (x) => x > 1); // true 18 | ``` 19 | -------------------------------------------------------------------------------- /snippets/compact.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: compact 3 | tags: list,beginner 4 | --- 5 | 6 | Removes falsy values from a list. 7 | 8 | - Use `Iterable.removeWhere()` in combination with the cascade operator (`..`) to filter out falsy values (`false`, `null`). 9 | 10 | ```dart 11 | List compact(List lst) { 12 | return lst..removeWhere((v) => [null, false].contains(v)); 13 | } 14 | ``` 15 | 16 | ```dart 17 | compact([0, 1, false, 2, 3, 'a', null, 'b']); // [0, 1, 2, 3, 'a', 'b'] 18 | ``` 19 | -------------------------------------------------------------------------------- /snippets/none.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: none 3 | tags: list,beginner 4 | --- 5 | 6 | Returns `true` if the provided predicate function returns `false` for all elements in a collection, `false` otherwise. 7 | 8 | - Use `Iterable.some()` to test if any elements in the collection return `true` based on `fn`. 9 | 10 | ```dart 11 | bool none(Iterable itr, bool Function(T) fn) { 12 | return !itr.any(fn); 13 | } 14 | ``` 15 | 16 | ```dart 17 | none([0, 1, 3, 0], (x) => x == 2); // true 18 | ``` 19 | -------------------------------------------------------------------------------- /snippets/reverseString.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: reverseString 3 | tags: string,beginner 4 | --- 5 | 6 | Reverses a string. 7 | 8 | - Use `String.split('')` and `Iterable.reversed` to reverse the order of the runes in the string. 9 | - Use `Iterable.join('')` to combine the runes and get the reversed string. 10 | 11 | ```dart 12 | String reverseString(String str) { 13 | return str.split('').reversed.join(''); 14 | } 15 | ``` 16 | 17 | ```dart 18 | reverseString('foobar'); // 'raboof' 19 | ``` 20 | -------------------------------------------------------------------------------- /snippets/nth.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: nth 3 | tags: list,beginner 4 | --- 5 | 6 | Returns the nth element of a list. 7 | 8 | - Check if `lst` has a length greater than `n`, use `lst[n]` if possible to return the nth element, otherwise return `null`. 9 | - Omit the second argument, `n`, to get the first element of the list. 10 | 11 | ```dart 12 | T nth(List lst, [n = 0]) { 13 | return lst.length > n ? lst[n] : null; 14 | } 15 | ``` 16 | 17 | ```dart 18 | nth(['a', 'b', 'c'], 1); // 'b' 19 | ``` 20 | -------------------------------------------------------------------------------- /snippets/some.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: some 3 | tags: list,beginner 4 | --- 5 | 6 | Returns `true` if the provided predicate function returns `true` for at least one element in a collection, `false` otherwise. 7 | 8 | - Use `Iterable.any()` to test if any elements in the collection return `true` based on `fn`. 9 | 10 | ```dart 11 | bool some(Iterable itr, bool Function(T) fn) { 12 | return itr.any(fn); 13 | } 14 | ``` 15 | 16 | ```dart 17 | some([0, 1, 2, 0], (x) => x >= 2); // true 18 | ``` 19 | -------------------------------------------------------------------------------- /snippets/flatten.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: flatten 3 | tags: list,recursion,intermediate 4 | --- 5 | 6 | Flattens a list. 7 | 8 | - Use recursion. 9 | - Use `Iterable.expand()` to combine elements into a single list, calling `flatten` recursively for any elements that are `List`s. 10 | 11 | ```dart 12 | List flatten(List lst) { 13 | return lst.expand((e) => e is List ? flatten(e) : [e]).toList(); 14 | } 15 | ``` 16 | 17 | ```dart 18 | flatten([1, [2], [[3], 4], 5]); // [1, 2, 3, 4, 5] 19 | ``` 20 | -------------------------------------------------------------------------------- /snippets/filterUnique.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: filterUnique 3 | tags: list,intermediate 4 | --- 5 | 6 | Filters out the unique values in a list. 7 | 8 | - Use `Iterable.where()` in combination with `List.indexOf()` and `List.lastIndexOf()` to filter out the unique values. 9 | 10 | ```dart 11 | List filterUnique(List lst) { 12 | return lst.where((i) => lst.indexOf(i) != lst.lastIndexOf(i)).toList(); 13 | } 14 | ``` 15 | 16 | ```dart 17 | filterUnique([1, 2, 2, 3, 4, 4, 5]); // [2, 2, 4, 4] 18 | ``` 19 | -------------------------------------------------------------------------------- /snippets/tail.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: tail 3 | tags: list,intermediate 4 | --- 5 | 6 | Returns all the elements of a list except the first one. 7 | 8 | - Check if `lst` has the appropriate length, use `List.sublist(1, lst.length)` if possible to return the result, otherwise return `lst`. 9 | 10 | ```dart 11 | List tail(List lst) { 12 | return lst.length > 1 ? lst.sublist(1, lst.length) : lst; 13 | } 14 | ``` 15 | 16 | ```dart 17 | tail([1, 2, 3]); // [2, 3] 18 | tail([1]); // [1] 19 | tail([]); // [] 20 | ``` 21 | -------------------------------------------------------------------------------- /snippets/union.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: union 3 | tags: list,math,beginner 4 | --- 5 | 6 | Returns every element that exists in any of the two lists once. 7 | 8 | - Use the plus operator (`+`) to concatenate `a` and `b`, `Iterable.toSet()` to get the unique values, `Iterable.toList()` to return a list. 9 | 10 | ```dart 11 | List union(Iterable a, Iterable b) { 12 | return (a.toList() + b.toList()).toSet().toList(); 13 | } 14 | ``` 15 | 16 | ```dart 17 | union([1, 2, 3], [4, 3, 2]); // [1, 2, 3, 4] 18 | ``` 19 | -------------------------------------------------------------------------------- /snippets/filterNonUnique.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: filterNonUnique 3 | tags: list,intermediate 4 | --- 5 | 6 | Filters out the non-unique values in a list. 7 | 8 | - Use `Iterable.where()` in combination with `List.indexOf()` and `List.lastIndexOf()` to filter out the non-unique values. 9 | 10 | ```dart 11 | List filterNonUnique(List lst) { 12 | return lst.where((i) => lst.indexOf(i) == lst.lastIndexOf(i)).toList(); 13 | } 14 | ``` 15 | 16 | ```dart 17 | filterNonUnique([1, 2, 2, 3, 4, 4, 5]); // [1, 3, 5] 18 | ``` 19 | -------------------------------------------------------------------------------- /snippets/initial.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: initial 3 | tags: list,intermediate 4 | --- 5 | 6 | Returns all the elements of a list except the last one. 7 | 8 | - Check if `lst` has the appropriate length, use `List.sublist(0, lst.length - 1)` if possible to return the result, otherwise return `lst`. 9 | 10 | ```dart 11 | List initial(List lst) { 12 | return lst.length > 1 ? lst.sublist(0, lst.length - 1) : lst; 13 | } 14 | ``` 15 | 16 | ```dart 17 | initial([1, 2, 3]); // [1, 2] 18 | initial([1]); // [1] 19 | initial([]); // [] 20 | ``` 21 | -------------------------------------------------------------------------------- /snippets/everyNth.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: everyNth 3 | tags: list,intermediate 4 | --- 5 | 6 | Returns every nth element in a list. 7 | 8 | - Use `List.generate()` to generate a list that fits the number of elements that will be returned, then add every `nth` element to it. 9 | 10 | ```dart 11 | import 'dart:math'; 12 | 13 | List everyNth(List lst, int nth) { 14 | return List.generate( 15 | (lst.length / nth).floor(), (i) => lst[(i + 1) * nth - 1]); 16 | } 17 | ``` 18 | 19 | ```dart 20 | everyNth([1, 2, 3, 4, 5, 6], 2); // [ 2, 4, 6 ] 21 | ``` 22 | -------------------------------------------------------------------------------- /snippets/digitize.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: digitize 3 | tags: math,list,beginner 4 | --- 5 | 6 | Converts an integer to a list of digits. 7 | 8 | - Use string interpolation to convert the integer to a string, `String.split('')` to convert it into a list. 9 | - Use `Iterable.map()` and `int.parse()` to transform each value to an integer, `Iterable.toList()` to return a list. 10 | 11 | ```dart 12 | List digitze(int n) { 13 | return "${n}".split('').map((s) => int.parse(s)).toList(); 14 | } 15 | ``` 16 | 17 | ```dart 18 | digitize(123); // [1, 2, 3] 19 | ``` 20 | -------------------------------------------------------------------------------- /snippets/includesAny.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: includesAny 3 | tags: list,beginner 4 | --- 5 | 6 | Returns `true` if any of the elements in `values` are included in `itr`, `false` otherwise. 7 | 8 | - Use `Iterable.any()` and `Iterable.contains()` to check if any elements of `values` are included in `itr`. 9 | 10 | ```dart 11 | bool includesAny(Iterable itr, Iterable values) { 12 | return values.any((v) => itr.contains(v)); 13 | } 14 | ``` 15 | 16 | ```dart 17 | includesAny([1, 2, 3, 4], [2, 9]); // true 18 | includesAny([1, 2, 3, 4], [8, 9]); // false 19 | ``` 20 | -------------------------------------------------------------------------------- /snippets/sumBy.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: sumBy 3 | tags: math,list,function,intermediate 4 | --- 5 | 6 | Returns the sum of a list, after mapping each element to a number using the provided function. 7 | 8 | - Use `Iterable.map()` to map each element to the numeric value returned by `fn`, `Iterable.reduce()` to sum the values. 9 | 10 | ```dart 11 | num sumBy(List lst, num Function(T) fn) { 12 | return lst.map(fn).reduce((num a, num b) => a + b); 13 | } 14 | ``` 15 | 16 | ```dart 17 | sumBy([ {'n': 4}, {'n': 2}, {'n': 8}, {'n': 6} ], (o) => o['n']); // 20 18 | ``` 19 | -------------------------------------------------------------------------------- /snippets/factorial.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: factorial 3 | tags: math,recursion,beginner 4 | --- 5 | 6 | Calculates the factorial of an integer. 7 | 8 | - Use recursion. 9 | - If `n` is less than or equal to `1`, return `1`. 10 | - Otherwise, return the product of `n` and the factorial of `n-1`. 11 | - Throws an exception if `n` is a negative number. 12 | 13 | ```dart 14 | int factorial(int n) { 15 | if (n < 0) throw ('Negative numbers are not allowed.'); 16 | return n <= 1 ? 1 : n * factorial(n - 1); 17 | } 18 | ``` 19 | 20 | ```dart 21 | factorial(6); // 720 22 | ``` 23 | -------------------------------------------------------------------------------- /snippets/includesAll.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: includesAll 3 | tags: list,beginner 4 | --- 5 | 6 | Returns `true` if all the elements in `values` are included in `itr`, `false` otherwise. 7 | 8 | - Use `Iterable.every()` and `Iterable.contains()` to check if all elements of `values` are included in `itr`. 9 | 10 | ```dart 11 | bool includesAll(Iterable itr, Iterable values) { 12 | return values.every((v) => itr.contains(v)); 13 | } 14 | ``` 15 | 16 | ```dart 17 | includesAll([1, 2, 3, 4], [1, 4]); // true 18 | includesAll([1, 2, 3, 4], [1, 5]); // false 19 | ``` 20 | -------------------------------------------------------------------------------- /snippets/compactWhitespace.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: compactWhitespace 3 | tags: string,regexp,beginner 4 | --- 5 | 6 | Returns a string with whitespaces compacted. 7 | 8 | - Use `String.replaceAll()` with a regular expression to replace all occurrences of 2 or more whitespace characters with a single space. 9 | 10 | ```dart 11 | String compactWhitespace(String str) { 12 | return str.replaceAll(RegExp(r'\s{2,}'), ' '); 13 | } 14 | ``` 15 | 16 | ```dart 17 | compactWhitespace('Lorem Ipsum'); // 'Lorem Ipsum' 18 | compactWhitespace('Lorem \n Ipsum'); // 'Lorem Ipsum' 19 | ``` 20 | -------------------------------------------------------------------------------- /snippets/maxBy.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: maxBy 3 | tags: math,list,function,intermediate 4 | --- 5 | 6 | Returns the maximum value of a list, after mapping each element to a number using the provided function. 7 | 8 | - Use `Iterable.map()` to map each element to the numeric value returned by `fn`, `max()` to find the maximum value. 9 | 10 | ```dart 11 | import 'dart:math'; 12 | 13 | num maxBy(List lst, num Function(T) fn) { 14 | return lst.map(fn).reduce(math.max); 15 | } 16 | ``` 17 | 18 | ```dart 19 | maxBy([ {'n': 4}, {'n': 2}, {'n': 8}, {'n': 6} ], (o) => o['n']); // 8 20 | ``` 21 | -------------------------------------------------------------------------------- /snippets/minBy.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: minBy 3 | tags: math,list,function,intermediate 4 | --- 5 | 6 | Returns the minimum value of a list, after mapping each element to a number using the provided function. 7 | 8 | - Use `Iterable.map()` to map each element to the numeric value returned by `fn`, `min()` to find the minimum value. 9 | 10 | ```dart 11 | import 'dart:math'; 12 | 13 | num minBy(List lst, num Function(T) fn) { 14 | return lst.map(fn).reduce(math.min); 15 | } 16 | ``` 17 | 18 | ```dart 19 | minBy([ {'n': 4}, {'n': 2}, {'n': 8}, {'n': 6} ], (o) => o['n']); // 8 20 | ``` 21 | -------------------------------------------------------------------------------- /snippets/mapList.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: mapList 3 | tags: list,intermediate 4 | --- 5 | 6 | Maps the values of a list using a function, where the key-value pairs consist of the value as the key and the mapped value. 7 | 8 | - Use `Map.fromIterable()` to generate a map with the original values as keys and their mapped values as values. 9 | 10 | ```dart 11 | Map mapList(Iterable itr, Y Function(T) fn) { 12 | return Map.fromIterable(itr, key: (i) => i, value: (i) => fn(i)); 13 | } 14 | ``` 15 | 16 | ```dart 17 | mapList([1, 2, 3], (a) => a * a); // [{1: 1, 2: 4, 3: 9}] 18 | ``` 19 | -------------------------------------------------------------------------------- /snippets/truncateString.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: truncateString 3 | tags: string,beginner 4 | --- 5 | 6 | Truncates a string up to a specified length. 7 | 8 | - Determine if the string's `length` is greater than `num`. 9 | - Return the string truncated to the desired length, with `'...'` appended to the end or the original string. 10 | 11 | ```dart 12 | String truncateString(String str, int num) { 13 | return str.length > num 14 | ? str.substring(0, num > 3 ? num - 3 : num) + '...' 15 | : str; 16 | } 17 | ``` 18 | 19 | ```dart 20 | truncateString('boomerang', 7); // 'boom...' 21 | ``` 22 | -------------------------------------------------------------------------------- /.github/config.yml: -------------------------------------------------------------------------------- 1 | # Configuration for request-info - https://github.com/behaviorbot/request-info 2 | 3 | # *Required* Comment to reply with 4 | requestInfoReplyComment: > 5 | We would appreciate it if you could provide us with some more information about this issue/PR! 6 | 7 | # *OPTIONAL* default titles to check against for lack of descriptiveness 8 | # MUST BE ALL LOWERCASE 9 | requestInfoDefaultTitles: 10 | - update readme.md 11 | - updates 12 | 13 | # *OPTIONAL* Label to be added to Issues and Pull Requests with insufficient information given 14 | requestInfoLabelToAdd: needs-more-info 15 | -------------------------------------------------------------------------------- /snippets/mostFrequent.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: mostFrequent 3 | tags: list,intermediate 4 | --- 5 | 6 | Returns the most frequent element in a list. 7 | 8 | - Use `Iterable.toSet()` to get the unique values of the list, `Set.reduce()` to iterate over them and `Iterable.where()` to find the most frequent element. 9 | 10 | ```dart 11 | T mostFrequent(Iterable itr) { 12 | return itr.toSet().reduce((i, j) => 13 | itr.where((v) => v == i).length > itr.where((v) => v == j).length ? i : j); 14 | } 15 | 16 | ``` 17 | 18 | ```dart 19 | mostFrequent(['a', 'b', 'a', 'c', 'a', 'a', 'b']); // 'a' 20 | ``` 21 | -------------------------------------------------------------------------------- /snippets/difference.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: difference 3 | tags: list,math,beginner 4 | --- 5 | 6 | Returns the difference between two lists. 7 | 8 | - Use `Iterable.toSet()` to get the unique values in `b`. 9 | - Use `Iterable.where()` in combination with `Iterable.contains()` to keep only the values in `a` not contained in `b`, `Iterable.toList()` to return the appropriate result. 10 | 11 | ```dart 12 | List difference(Iterable a, Iterable b) { 13 | final s = b.toSet(); 14 | return a.where((x) => !s.contains(x)).toList(); 15 | } 16 | ``` 17 | 18 | ```dart 19 | difference([1, 2, 3], [1, 2, 4]); // [3] 20 | ``` 21 | -------------------------------------------------------------------------------- /snippets/averageBy.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: averageBy 3 | tags: math,list,function,intermediate 4 | --- 5 | 6 | Returns the average of a list, after mapping each element to a number using the provided function. 7 | 8 | - Use `Iterable.map()` to map each element to the numeric value returned by `fn`, `Iterable.reduce()` to sum the values, divide by `Iterable.length` to get the average. 9 | 10 | ```dart 11 | num averageBy(List lst, num Function(T) fn) { 12 | return lst.map(fn).reduce((num a, num b) => a + b) / lst.length; 13 | } 14 | ``` 15 | 16 | ```dart 17 | averageBy([ {'n': 4}, {'n': 2}, {'n': 8}, {'n': 6} ], (o) => o['n']); // 5 18 | ``` 19 | -------------------------------------------------------------------------------- /snippets/intersection.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: intersection 3 | tags: list,math,beginner 4 | --- 5 | 6 | Returns a list of elements that exist in both lists. 7 | 8 | - Use `Iterable.toSet()` to get the unique values in `b`. 9 | - Use `Iterable.toSet()`, `Iterable.where()` and `Iterable.contains()` to keep only the values in `a` contained in `b`, `Iterable.toList()` to return the appropriate result. 10 | 11 | ```dart 12 | List intersection(Iterable a, Iterable b) { 13 | final s = b.toSet(); 14 | return a.toSet().where((x) => s.contains(x)).toList(); 15 | } 16 | ``` 17 | 18 | ```dart 19 | intersection([1, 2, 3], [1, 2, 4]); // [1, 2] 20 | ``` 21 | -------------------------------------------------------------------------------- /snippets/mapString.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: mapString 3 | tags: string,list,function,utility,beginner 4 | --- 5 | 6 | Creates a string with the results of calling the provided function on every character in the given string. 7 | 8 | - Use `String.split('')` and `Iterable.map()` to call the provided function, `fn`, for each character in `str`. 9 | - Use `Iterable.join('')` to recombine the list of runes into a string. 10 | 11 | ```dart 12 | String mapString(String str, String Function(String c) fn) { 13 | return str.split('').map(fn).join(''); 14 | } 15 | ``` 16 | 17 | ```dart 18 | mapString('lorem ipsum', (c) => c.toUpperCase()); // 'LOREM IPSUM' 19 | ``` 20 | -------------------------------------------------------------------------------- /snippets/fibonacci.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: fibonacci 3 | tags: math,list,beginner 4 | --- 5 | 6 | Generates a list, containing the Fibonacci sequence, up until the nth term. 7 | 8 | - Use `List.generate()` to generate a list with `n` terms, using a function that returns the sum of the last two values, except for the first two. 9 | 10 | ```dart 11 | List fibonacci(int n) { 12 | int last = 1; 13 | int last2 = 0; 14 | return List.generate(n, (int i) { 15 | if (i < 2) return i; 16 | int curr = last + last2; 17 | last2 = last; 18 | last = curr; 19 | return curr; 20 | }); 21 | } 22 | ``` 23 | 24 | ```dart 25 | fibonacci(6); // [0, 1, 1, 2, 3, 5] 26 | ``` 27 | -------------------------------------------------------------------------------- /snippets/unzip.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: unzip 3 | tags: list,intermediate 4 | --- 5 | 6 | Creates a list of lists, ungrouping the elements in a list produced by [zip](/dart/s/zip). 7 | 8 | - Use `List.generate()` twice to generate a list of lists, using the appropriate indexes to get their values from the original list. 9 | 10 | ```dart 11 | List> unzip(List> itr) { 12 | return List.generate( 13 | itr[0].length, (i) => List.generate(itr.length, (k) => itr[k][i])); 14 | } 15 | ``` 16 | 17 | ```dart 18 | unzip([['a', 1, true], ['b', 2, false]]); // [['a', 'b'], [1, 2], [true, false]] 19 | unzip([['a', 1, true], ['b', 2]]); // [['a', 'b'], [1, 2], [true]] 20 | ``` 21 | -------------------------------------------------------------------------------- /snippets/isPalindrome.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: isPalindrome 3 | tags: string,intermediate 4 | --- 5 | 6 | Returns `true` if the given string is a palindrome, `false` otherwise. 7 | 8 | - Use `String.toLowerCase()` to convert the given string to lowercase, `String.replaceAll()` to remove non-alphanumeric characters. 9 | - Use `String.split('')`, `Iterable.reversed` and `Iterable.join('')` to reverse it and compare it to the unreversed string. 10 | 11 | ```dart 12 | bool isPalindrome(String str) { 13 | String s = str.toLowerCase().replaceAll(RegExp(r'[\W_]'), ''); 14 | return s == s.split('').reversed.join(''); 15 | } 16 | ``` 17 | 18 | ```dart 19 | isPalindrome('taco cat'); // true 20 | ``` 21 | -------------------------------------------------------------------------------- /snippets/frequencies.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: frequencies 3 | tags: list,intermediate 4 | --- 5 | 6 | Returns a map with the unique values of a list as keys and their frequencies as the values. 7 | 8 | - Use `Iterable.toSet()` to get the unique values of the list. 9 | - Use `Map.fromIterable()`, `Iterable.where()` and `Iterable.length` to generate a map with the unique values as keys and their frequencies as values. 10 | 11 | ```dart 12 | Map frequencies(Iterable itr) { 13 | return Map.fromIterable(itr.toSet(), 14 | value: (i) => itr.where((v) => v == i).length); 15 | } 16 | ``` 17 | 18 | ```dart 19 | frequencies(['a', 'b', 'a', 'c', 'a', 'a', 'b']); // { a: 4, b: 2, c: 1 } 20 | ``` 21 | -------------------------------------------------------------------------------- /snippets/dropWhile.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: dropWhile 3 | tags: list,intermediate 4 | --- 5 | 6 | Removes elements from the start of a list until the passed function returns `true` and returns the remaining elements. 7 | 8 | - Use `List.indexWhere()` with the `test` function to find the first element that returns `true`. 9 | - Return an empty array if there are no matching elements, otherwise use `List.sublist()` to return the remaining elements. 10 | 11 | ```dart 12 | List dropWhile(List lst, bool Function(T) test) { 13 | int leftIndex = lst.indexWhere(test); 14 | return leftIndex == -1 ? [] : lst.sublist(leftIndex); 15 | } 16 | ``` 17 | 18 | ```dart 19 | dropWhile([1, 2, 3, 4], (n) => n >= 3); // [3, 4] 20 | ``` 21 | -------------------------------------------------------------------------------- /snippets/pad.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: pad 3 | tags: string,beginner 4 | --- 5 | 6 | Pads a string on both sides with the specified `padding`, if it's shorter than the specified `length`. 7 | 8 | - Use `String.padLeft()` and `String.padRight()` to pad both sides of the given string. 9 | - Omit the optional parameter, `padding`, to use the whitespace as the default padding. 10 | 11 | ```dart 12 | String pad(String str, int length, {String padding = ' '}) { 13 | return str 14 | .padLeft(((str.length + length) / 2).floor(), padding) 15 | .padRight(length, padding); 16 | } 17 | ``` 18 | 19 | ```dart 20 | pad('cat', 8); // ' cat ' 21 | pad(String(42), 6, padding: '0'); // '004200' 22 | pad('foobar', 3); // 'foobar' 23 | ``` 24 | -------------------------------------------------------------------------------- /snippets/capitalize.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: capitalize 3 | tags: string,beginner 4 | --- 5 | 6 | Capitalizes the first letter of a string. 7 | 8 | - Use `String.toUpperCase()` to capitalize first letter and `String.toLowerCase()` to convert the rest of the string to lowercase, if necessary. 9 | - Omit the optional parameter, `lowerRest`, to keep the rest of the string intact, or set it to `true` to convert to lowercase. 10 | 11 | ```dart 12 | String capitalize(String str, {bool lowerRest = false}) { 13 | return str[0].toUpperCase() + 14 | (lowerRest ? str.substring(1).toLowerCase() : str.substring(1)); 15 | } 16 | ``` 17 | 18 | ```dart 19 | capitalize('fooBar'); // 'FooBar' 20 | capitalize('fooBar', lowerRest: true); // 'Foobar' 21 | ``` 22 | -------------------------------------------------------------------------------- /snippets/decapitalize.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: decapitalize 3 | tags: string,beginner 4 | --- 5 | 6 | Decapitalizes the first letter of a string. 7 | 8 | - Use `String.toLowerCase()` to decapitalize first letter and `String.toUpperCase()` to convert the rest of the string to uppercase, if necessary. 9 | - Omit the optional parameter, `upperRest`, to keep the rest of the string intact, or set it to `true` to convert to uppercase. 10 | 11 | ```dart 12 | String capitalize(String str, {bool upperRest = false}) { 13 | return str[0].toUpperCase() + 14 | (upperRest ? str.substring(1).toLowerCase() : str.substring(1)); 15 | } 16 | ``` 17 | 18 | ```dart 19 | capitalize('FooBar'); // 'fooBar' 20 | capitalize('FooBar', upperRest: true); // 'fOOBAR' 21 | ``` 22 | -------------------------------------------------------------------------------- /snippets/dropRightWhile.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: dropRightWhile 3 | tags: list,intermediate 4 | --- 5 | 6 | Removes elements from the end of a list until the passed function returns `true` and returns the remaining elements. 7 | 8 | - Use `List.lastIndexWhere()` with the `test` function to find the last element that returns `true`. 9 | - Return an empty array if there are no matching elements, otherwise use `List.sublist()` to return the remaining elements. 10 | 11 | ```dart 12 | List dropRightWhile(List lst, bool Function(T) test) { 13 | int rightIndex = lst.lastIndexWhere(test); 14 | return rightIndex == -1 ? [] : lst.sublist(0, rightIndex + 1); 15 | } 16 | ``` 17 | 18 | ```dart 19 | dropRightWhile([1, 2, 3, 4], (n) => n < 3); // [1, 2] 20 | ``` 21 | -------------------------------------------------------------------------------- /snippets/chunk.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: chunk 3 | tags: list,intermediate 4 | --- 5 | 6 | Chunks a list into smaller lists of the specified size. 7 | 8 | - Use `List.generate()` to generate a list that fits the number of chunks that will be produced. 9 | - Use `List.sublist()` to map each element of the new list to a chunk the length of `size`. 10 | - If the original `list` can't be split evenly, the final chunk will contain the remaining elements. 11 | 12 | ```dart 13 | import 'dart:math'; 14 | 15 | List> chunk(List lst, int size) { 16 | return List.generate((lst.length / size).ceil(), 17 | (i) => lst.sublist(i * size, min(i * size + size, lst.length))); 18 | } 19 | ``` 20 | 21 | ```dart 22 | chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]] 23 | ``` 24 | -------------------------------------------------------------------------------- /snippets/groupBy.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: groupBy 3 | tags: list,intermediate 4 | --- 5 | 6 | Groups the elements of a list based on the given function. 7 | 8 | - Use `Iterable.map()` to map each element to the value returned by `fn`, `Iterable.toSet()` to get the unique values of the list. 9 | - Use `Map.fromIterable()`, `Iterable.where()` and `Iterable.toList()` to generate a map with the unique values as keys and the list elements as values. 10 | 11 | ```dart 12 | Map> groupBy(Iterable itr, Y Function(T) fn) { 13 | return Map.fromIterable(itr.map(fn).toSet(), 14 | value: (i) => itr.where((v) => fn(v) == i).toList()); 15 | } 16 | ``` 17 | 18 | ```dart 19 | groupBy(['one', 'two', 'three'], (v) => v.length); // {3: ['one', 'two'], 5: ['three']} 20 | ``` 21 | -------------------------------------------------------------------------------- /.github/stale.yml: -------------------------------------------------------------------------------- 1 | # Number of days of inactivity before an issue becomes stale 2 | daysUntilStale: 21 3 | # Number of days of inactivity before a stale issue is closed 4 | daysUntilClose: 7 5 | # Issues with these labels will never be considered stale 6 | exemptLabels: 7 | - not-stale 8 | - pinned 9 | - security 10 | # Label to use when marking an issue as stale 11 | staleLabel: false 12 | # Comment to post when marking an issue as stale. Set to `false` to disable 13 | markComment: > 14 | This issue has been automatically marked as stale because it has not had 15 | recent activity. It will be closed if no further activity occurs. Thank you 16 | for your contributions. 17 | # Comment to post when closing a stale issue. Set to `false` to disable 18 | closeComment: false 19 | -------------------------------------------------------------------------------- /snippets/randomIntInRange.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: randomIntInRange 3 | tags: math,utility,random,beginner 4 | --- 5 | 6 | Returns a random integer in the specified range. 7 | 8 | - Use `Random.nextDouble()` to generate a random number between `0.0` and `1.0` and map it to the desired range, using `num.floor()` to make it an integer. 9 | - Omit the optional parameter, `min`, to use a default minimu value of `0`. 10 | - Omit the optional parameter, `max`, to use a default maximum value of `100`. 11 | 12 | ```dart 13 | import 'dart:math'; 14 | 15 | int randomIntInRange({int min = 0, int max = 100}) { 16 | return (Random().nextDouble() * (max - min + 1) + min).floor(); 17 | } 18 | ``` 19 | 20 | ```dart 21 | randomIntInRange(); // 90 22 | randomIntInRange(min: 10, max: 30); // 23 23 | ``` 24 | -------------------------------------------------------------------------------- /snippets/randomDoubleInRange.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: randomDoubleInRange 3 | tags: math,utility,random,beginner 4 | --- 5 | 6 | Returns a random double in the specified range. 7 | 8 | - Use `Random.nextDouble()` to generate a random number between `0.0` and `1.0` and map it to the desired range. 9 | - Omit the optional parameter, `min`, to use a default minimu value of `0.0`. 10 | - Omit the optional parameter, `max`, to use a default maximum value of `1.0`. 11 | 12 | ```dart 13 | import 'dart:math'; 14 | 15 | double randomDoubleInRange({double min = 0.0, double max = 1.0}) { 16 | return Random().nextDouble() * (max - min + 1) + min; 17 | } 18 | ``` 19 | 20 | ```dart 21 | randomDoubleInRange(); // 0.719213632334785 22 | randomDoubleInRange(min: 2.4, max: 9.8); // 6.21315328537085 23 | ``` 24 | -------------------------------------------------------------------------------- /snippets/countBy.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: countBy 3 | tags: list,intermediate 4 | --- 5 | 6 | Groups the elements of a list based on the given function and returns the count of elements in each group. 7 | 8 | - Use `Iterable.map()` to map each element to the value returned by `fn`, `Iterable.toSet()` to get the unique values of the list. 9 | - Use `Map.fromIterable()`, `Iterable.where()` and `Iterable.length` to generate a map with the unique values as keys and their frequencies as values. 10 | 11 | ```dart 12 | Map countBy(Iterable itr, Y Function(T) fn) { 13 | return Map.fromIterable(itr.map(fn).toSet(), 14 | value: (i) => itr.where((v) => fn(v) == i).length); 15 | } 16 | ``` 17 | 18 | ```dart 19 | countBy(['one', 'two', 'three'], (v) => v.length); // [{3: 2, 5: 1}] 20 | ``` 21 | -------------------------------------------------------------------------------- /snippets/haveSameContents.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: haveSameContents 3 | tags: list,intermediate 4 | --- 5 | 6 | Returns `true` if two lists contain the same elements regardless of order, `false` otherwise. 7 | 8 | 9 | - Use the plus operator (`+`) to concatenate `a` and `b`, `Iterable.toSet()` to get all the unique values. 10 | - Use a `for..in` loop and compare the occurence count of each value between the two lists. 11 | - Return `false` if any occurence count doesn't match, `true` otherwise. 12 | 13 | ```dart 14 | bool haveSameContents(List a, List b) { 15 | for (T v in (a + b).toSet()) 16 | if (a.where((e) => e == v).length != b.where((e) => e == v).length) 17 | return false; 18 | return true; 19 | } 20 | ``` 21 | 22 | ```dart 23 | haveSameContents([1, 2, 4], [2, 4, 1]); // true 24 | ``` 25 | -------------------------------------------------------------------------------- /snippets/mask.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: mask 3 | tags: string,utility,intermediate 4 | --- 5 | 6 | Replaces all but the last `num` runes of a string with the specified `mask`. 7 | 8 | - Use `String.substring()` to grab the last `num` runes, `String.padLeft()` to fill the beginning of the string with the `mask` up to the original length. 9 | - Omit the optional parameter, `num`, to keep a default of `4` runes unmasked. 10 | - Omit the optional parameter, `mask`, to use the default of `'*'`. 11 | 12 | ```dart 13 | String mask(String str, {int num = 4, String mask = '*'}) { 14 | return str.substring(str.length - num).padLeft(str.length, mask); 15 | } 16 | ``` 17 | 18 | ```dart 19 | mask('1234567890'); // '******7890' 20 | mask('1234567890', num: 3); // '*******890' 21 | mask('1234567890', num: 4, mask: '\$'); // '$$$$$$7890' 22 | ``` 23 | -------------------------------------------------------------------------------- /snippets/randomIntListInRange.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: randomIntListInRange 3 | tags: math,utility,random,intermediate 4 | --- 5 | 6 | Returns a a list of `n` random integers in the specified range. 7 | 8 | - Use `List.generate()` to generate a list with `n` terms, using a function that returns a random integer between `min` and `max`. 9 | - Omit the optional parameter, `min`, to use a default minimu value of `0`. 10 | - Omit the optional parameter, `max`, to use a default maximum value of `100`. 11 | 12 | ```dart 13 | import 'dart:math'; 14 | 15 | List randomIntListInRange(n, {int min = 0, int max = 100}) { 16 | return List.generate( 17 | n, (_) => (Random().nextDouble() * (max - min + 1) + min).floor()); 18 | } 19 | ``` 20 | 21 | ```dart 22 | randomIntListInRange(12, 35, 10); // [ 34, 14, 27, 17, 30, 27, 20, 26, 21, 14 ] 23 | ``` 24 | -------------------------------------------------------------------------------- /snippets/words.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: words 3 | tags: string,regexp,intermediate 4 | --- 5 | 6 | Converts a given string into a list of words. 7 | 8 | - Use `String.split()` with the supplied `pattern` to convert to a list of strings. 9 | - Use `Iterable.where()` in combination with `String.isNotEmpty` to remove any empty strings. 10 | - Finally, convert to a list using `Iterable.toList()`. 11 | - Omit the optional parameter, `pattern`, to use the default regular expression (non-alphanumeric characters). 12 | 13 | ```dart 14 | List words(String str, {String pattern = '[^a-zA-Z-]+'}) { 15 | return str.split(RegExp(pattern)).where((s) => s.isNotEmpty).toList(); 16 | } 17 | ``` 18 | 19 | ```dart 20 | words('I love dart!!'); // ['I', 'love', 'dart'] 21 | words('JavaScript, TypeScript & Dart'); // ['JavaScript', 'TypeScript', 'Dart'] 22 | ``` 23 | -------------------------------------------------------------------------------- /snippets/bifurcateBy.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: bifurcateBy 3 | tags: list,intermediate 4 | --- 5 | 6 | Splits values into two groups according to a predicate function, which specifies which group an element in the input collection belongs to. 7 | If the predicate function returns `true`, the collection element belongs to the first group; otherwise, it belongs to the second group. 8 | 9 | - Use `Iterable.retainWhere()` and `Iterable.removeWhere()` in combination with the cascade operator (`..`) and `List.from()` to create the appropriate groups using the `filter` function. 10 | 11 | ```dart 12 | List> bifurcateBy(List lst, bool Function(T) filter) { 13 | return [ 14 | List.from(lst..retainWhere(filter)), 15 | List.from(lst..removeWhere(filter)) 16 | ]; 17 | } 18 | ``` 19 | 20 | ```dart 21 | bifurcateBy(['beep', 'boop', 'foo', 'bar'], (x) => x[0] == 'b'); // [['beep', 'boop', 'bar'], ['foo']] 22 | ``` 23 | -------------------------------------------------------------------------------- /snippets/symmetricDifference.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: symmetricDifference 3 | tags: list,math,intermediate 4 | --- 5 | 6 | Returns the symmetric difference between two lists, without filtering out duplicate values. 7 | 8 | - Use `Iterable.toSet()` to get the unique values in each list. 9 | - Use `Iterable.where()` in combination with `Iterable.contains()` to keep only the values in one list and not the other. 10 | - Finally, use `Iterable.toList()` and `Iterable.addAll()` in combination with the cascade operator (`..`) to return the result. 11 | 12 | ```dart 13 | List symmetricDifference(List a, List b) { 14 | final sA = a.toSet(), sB = b.toSet(); 15 | return a.where((x) => !sB.contains(x)).toList() 16 | ..addAll(b.where((x) => !sA.contains(x))); 17 | } 18 | ``` 19 | 20 | ```dart 21 | symmetricDifference([1, 2, 3], [1, 2, 4]); // [3, 4] 22 | symmetricDifference([1, 2, 2], [1, 3, 1]); // [2, 2, 3] 23 | ``` 24 | -------------------------------------------------------------------------------- /snippets/zip.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: zip 3 | tags: list,intermediate 4 | --- 5 | 6 | Creates a list of elements, grouped based on the position in the original lists. 7 | 8 | - Use `List.generate()` to generate a list, using `Iterable.map()`, `Iterable.reduce()` and `max()` to get the longest sublist. 9 | - Use `List.generate()` inside the iterator to generate the sublists, using the appropriate indexes to get their values from the original list. 10 | 11 | ```dart 12 | import 'dart:math'; 13 | 14 | List> zip(List> itr) { 15 | return List.generate( 16 | itr.map((x) => x.length).reduce(max), 17 | (i) => List.generate( 18 | itr.length, (k) => itr[k].length > i ? itr[k][i] : null)); 19 | } 20 | ``` 21 | 22 | ```dart 23 | zip([['a', 'b'], [1, 2], [true, false]]); // [['a', 1, true], ['b', 2, false]] 24 | zip([['a'], [1, 2], [true, false]]); // [['a', 1, true], [null, 2, false]] 25 | ``` 26 | -------------------------------------------------------------------------------- /snippets/differenceBy.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: differenceBy 3 | tags: list,math,intermediate 4 | --- 5 | 6 | Returns the difference between two lists, after applying the provided function to each list element of both. 7 | 8 | - Use `Iterable.toSet()` and `Iterable.map()` to get the unique values in `b` after applying `fn` to them. 9 | - Use `Iterable.map()` to apply `fn` to all the values of `a`, `Iterable.where()` in combination with `Iterable.contains()` to keep only the values in the resulting list not contained in the unique mapped values of `b`, `Iterable.toList()` to return the appropriate result. 10 | 11 | ```dart 12 | List differenceBy(Iterable a, Iterable b, Y Function(T) fn) { 13 | final s = b.map(fn).toSet(); 14 | return a.map((x) => fn(x)).where((x) => !s.contains(x)).toList(); 15 | } 16 | ``` 17 | 18 | ```dart 19 | differenceBy([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], (v) => v['x']); // [2] 20 | ``` 21 | -------------------------------------------------------------------------------- /snippets/intersectionBy.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: intersectionBy 3 | tags: list,math,intermediate 4 | --- 5 | 6 | Returns a list of elements that exist in both lists, after applying the provided function to each element of both. 7 | 8 | - Use `Iterable.toSet()` and `Iterable.map()` to get the unique values in `b` after applying `fn` to them. 9 | - Use `Iterable.map()` to apply `fn` to all the values of `a`, `Iterable.toSet()`, `Iterable.where()` and `Iterable.contains()` to keep only the values in the resulting list contained in the unique mapped values of `b`, `Iterable.toList()` to return the appropriate result. 10 | 11 | ```dart 12 | List intersectionBy(Iterable a, Iterable b, Y Function(T) fn) { 13 | final s = b.map(fn).toSet(); 14 | return a.toSet().map((x) => fn(x)).where((x) => s.contains(x)).toList(); 15 | } 16 | ``` 17 | 18 | ```dart 19 | intersectionBy([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], (v) => v['x']); // [1] 20 | ``` 21 | -------------------------------------------------------------------------------- /snippets/toKebabCase.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: toKebabCase 3 | tags: string,regexp,intermediate 4 | --- 5 | 6 | Converts a string to kebab case. 7 | 8 | - Use `String.replaceAllMapped()` to break the string into words and `String.toLowerCase()` to convert each one to lowercase, using a `RegExp`. 9 | - Use `String.replaceAll()` to replace invalid separator characters (`_` and spaces). with hyphens 10 | 11 | ```dart 12 | String toKebabCase(String str) { 13 | return str 14 | .replaceAllMapped( 15 | RegExp( 16 | r'[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+'), 17 | (Match m) => "${m[0].toLowerCase()}") 18 | .replaceAll(RegExp(r'(_|\s)+'), '-'); 19 | } 20 | ``` 21 | 22 | ```dart 23 | toKebabCase('camelCase'); // 'camel-case' 24 | toKebabCase('some text'); // 'some-text' 25 | toKebabCase('some-mixed_string With spaces_underscores-and-hyphens'); // 'some-mixed-string-with-spaces-underscores-and-hyphens' 26 | ``` 27 | -------------------------------------------------------------------------------- /snippets/toSnakeCase.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: toSnakeCase 3 | tags: string,regexp,intermediate 4 | --- 5 | 6 | Converts a string to snake case. 7 | 8 | - Use `String.replaceAllMapped()` to break the string into words and `String.toLowerCase()` to convert each one to lowercase, using a `RegExp`. 9 | - Use `String.replaceAll()` to replace invalid separator characters (`-` and spaces) with underscores. 10 | 11 | ```dart 12 | String toSnakeCase(String str) { 13 | return str 14 | .replaceAllMapped( 15 | RegExp( 16 | r'[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+'), 17 | (Match m) => "${m[0].toLowerCase()}") 18 | .replaceAll(RegExp(r'(-|\s)+'), '_'); 19 | } 20 | ``` 21 | 22 | ```dart 23 | toSnakeCase('camelCase'); // 'camel_case' 24 | toSnakeCase('some text'); // 'some_text' 25 | toSnakeCase('some-mixed_string With spaces_underscores-and-hyphens'); // 'some_mixed_string_with_spaces_underscores_and_hyphens' 26 | ``` 27 | -------------------------------------------------------------------------------- /snippets/isAnagram.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: isAnagram 3 | tags: string,regexp,intermediate 4 | --- 5 | 6 | Checks if a string is an anagram of another string (case-insensitive, ignores spaces, punctuation and special characters). 7 | 8 | - Use `String.toLowerCase()` and `String.prototype.replaceAll()` with an appropriate regular expression to remove unnecessary characters and convert the string to lowercase. 9 | - Use `String.split('')`, `Iterable.sort()`, in combination with the cascade operator (`..`) and `Iterable.join('')` to normalize both strings and check if their normalized forms are equal. 10 | 11 | ```dart 12 | bool isAnagram(String str1, String str2) { 13 | String normalize(String str) => (str 14 | .toLowerCase() 15 | .replaceAll(RegExp(r'[^a-z0-9]', caseSensitive: false), '') 16 | .split('') 17 | ..sort()) 18 | .join(''); 19 | return normalize(str1) == normalize(str2); 20 | } 21 | ``` 22 | 23 | ```dart 24 | isAnagram('iceman', 'cinema'); // true 25 | ``` 26 | -------------------------------------------------------------------------------- /snippets/slice.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: slice 3 | tags: list,intermediate 4 | --- 5 | 6 | Returns a new list containing the elements between `start` and `end`. 7 | Negative values can be used, indicating an offset from the end of the list. 8 | 9 | - Use `num.isNegative` to check if either `start` or `end` are negative and normalize their values. 10 | - Use `List.sublist()` with the normalized values to return the desired list. 11 | - Omit the optional parameter, `end`, to use the length of the list as the default. 12 | 13 | ```dart 14 | List slice(List lst, int start, [int end]) { 15 | int _start = start.isNegative ? lst.length + start : start; 16 | int _end = end != null ? end.isNegative ? lst.length + end : end : lst.length; 17 | return lst.sublist(_start, _end); 18 | } 19 | ``` 20 | 21 | ```dart 22 | List n = [1, 2, 3, 4, 5, 6, 7, 8]; 23 | 24 | slice(n, 5); // [6, 7, 8] 25 | slice(n, 1, 3); // [2, 3] 26 | slice(n, 2, -2); // [3, 4, 5, 6] 27 | slice(n, -4); // [5, 6, 7, 8] 28 | slice(n, -6, -2); // [3, 4, 5, 6] 29 | ``` 30 | -------------------------------------------------------------------------------- /snippets/unionBy.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: unionBy 3 | tags: list,math,intermediate 4 | --- 5 | 6 | Returns every element that exists in any of the two lists once, after applying the provided function to each element of both. 7 | 8 | - Use `Iterable.toSet()` and `Iterable.map()` to get the unique values in `a` after applying `fn` to them. 9 | - Use `Iterable.map()` to apply `fn` to all the values of `b`, `Iterable.toSet()`, `Iterable.where()` and `Iterable.contains()` to keep only the values in the resulting list contained in the unique mapped values of `a`, `Iterable.toList()` to return the appropriate result. 10 | - Finally, use the plus operator (`+`) to concatenate the two lists and return the result. 11 | 12 | ```dart 13 | List unionBy(Iterable a, Iterable b, Y Function(T) fn) { 14 | final sA = a.map(fn).toSet().toList(); 15 | final sB = b.map(fn).toSet().where((x) => !sA.contains(x)).toList(); 16 | return sA + sB; 17 | } 18 | ``` 19 | 20 | ```dart 21 | unionBy([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], (v) => v['x']); // [2, 1] 22 | ``` 23 | -------------------------------------------------------------------------------- /snippets/symmetricDifferenceBy.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: symmetricDifferenceBy 3 | tags: list,math,intermediate 4 | --- 5 | 6 | Returns the symmetric difference between two lists, after applying the provided function to each list element of both. 7 | 8 | - Use `Iterable.toSet()` and `Iterable.map()` to get the unique values in each list after applying `fn` to them. 9 | - Use `Iterable.where()` in combination with `Iterable.contains()` to keep only the values in one list and not the other. 10 | - Finally, use `Iterable.toList()` and `Iterable.addAll()` in combination with the cascade operator (`..`) to return the result. 11 | 12 | ```dart 13 | List symmetricdifferenceBy(List a, List b, Y Function(T) fn) { 14 | final sA = a.map(fn).toSet(), sB = b.map(fn).toSet(); 15 | return a.map((x) => fn(x)).where((x) => !sB.contains(x)).toList() 16 | ..addAll(b.map((x) => fn(x)).where((x) => !sA.contains(x))); 17 | } 18 | ``` 19 | 20 | ```dart 21 | symmetricdifferenceBy([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }, { 'x': 3 }], (v) => v['x']); // [2, 3] 22 | ``` 23 | -------------------------------------------------------------------------------- /snippets/toTitleCase.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: toTitleCase 3 | tags: string,regexp,intermediate 4 | --- 5 | 6 | Converts a string to title case. 7 | 8 | - Use `String.replaceAllMapped()` to break the string into words and capitalize the first letter of each word, using a `RegExp`. 9 | - Use `String.replaceAll()` to replace invalid separator characters (`-` and `_`) with spaces. 10 | 11 | ```dart 12 | String toTitleCase(String str) { 13 | return str 14 | .replaceAllMapped( 15 | RegExp( 16 | r'[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+'), 17 | (Match m) => 18 | "${m[0][0].toUpperCase()}${m[0].substring(1).toLowerCase()}") 19 | .replaceAll(RegExp(r'(_|-)+'), ' '); 20 | } 21 | ``` 22 | 23 | ```dart 24 | toTitleCase('some_database_field_name'); // 'Some Database Field Name' 25 | toTitleCase('Some label that needs to be title-cased'); // 'Some Label That Needs To Be Title Cased' 26 | toTitleCase('some-package-name'); // 'Some Package Name' 27 | toTitleCase('some-mixed_string with spaces_underscores-and-hyphens'); // 'Some Mixed String With Spaces Underscores And Hyphens' 28 | ``` 29 | -------------------------------------------------------------------------------- /.github/lock.yml: -------------------------------------------------------------------------------- 1 | # Configuration for Lock Threads - https://github.com/dessant/lock-threads 2 | 3 | # Number of days of inactivity before a closed issue or pull request is locked 4 | daysUntilLock: 60 5 | 6 | # Skip issues and pull requests created before a given timestamp. Timestamp must 7 | # follow ISO 8601 (`YYYY-MM-DD`). Set to `false` to disable 8 | skipCreatedBefore: false 9 | 10 | # Issues and pull requests with these labels will be ignored. Set to `[]` to disable 11 | exemptLabels: [] 12 | 13 | # Label to add before locking, such as `outdated`. Set to `false` to disable 14 | lockLabel: false 15 | 16 | # Comment to post before locking. Set to `false` to disable 17 | lockComment: false 18 | 19 | # Assign `resolved` as the reason for locking. Set to `false` to disable 20 | setLockReason: true 21 | 22 | # Limit to only `issues` or `pulls` 23 | # only: issues 24 | 25 | # Optionally, specify configuration settings just for `issues` or `pulls` 26 | # issues: 27 | # exemptLabels: 28 | # - help-wanted 29 | # lockLabel: outdated 30 | 31 | # pulls: 32 | # daysUntilLock: 30 33 | 34 | # Repository to extend settings from 35 | # _extends: repo 36 | -------------------------------------------------------------------------------- /snippets/toCamelCase.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: toCamelCase 3 | tags: string,regexp,intermediate 4 | --- 5 | 6 | Converts a string to camelcase. 7 | 8 | - Use `String.replaceAllMapped()` to break the string into words and capitalize the first letter of each word, using a `RegExp`. 9 | - Use `String.replaceAll()` to remove invalid separator characters (`_`, `-` and spaces). 10 | - Finally, use `String.toLowerCase()` and to convert the first letter to lowercase. 11 | 12 | ```dart 13 | String toCamelCase(String str) { 14 | String s = str 15 | .replaceAllMapped( 16 | RegExp( 17 | r'[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+'), 18 | (Match m) => 19 | "${m[0][0].toUpperCase()}${m[0].substring(1).toLowerCase()}") 20 | .replaceAll(RegExp(r'(_|-|\s)+'), ''); 21 | return s[0].toLowerCase() + s.substring(1); 22 | } 23 | ``` 24 | 25 | ```dart 26 | toCamelCase('some_database_field_name'); // 'someDatabaseFieldName' 27 | toCamelCase('Some label that needs to be camelized'); // 'someLabelThatNeedsToBeCamelized' 28 | toCamelCase('some-javascript-property'); // 'someJavascriptProperty' 29 | toCamelCase('some-mixed_string with spaces_underscores-and-hyphens'); // 'someMixedStringWithSpacesUnderscoresAndHyphens' 30 | ``` 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Logo](/logo.png)](https://30secondsofcode.org/dart/p/1) 2 | 3 | # 30 seconds of Dart 4 | 5 | > Short Dart code snippets for all your development needs 6 | 7 | * Visit [our website](https://30secondsofcode.org) to view our snippet collection. 8 | * Use the [Search page](https://30secondsofcode.org/search) to find snippets that suit your needs. You can search by name, tag, language or using a snippet's description. Just start typing a term and see what comes up. 9 | * Browse the [Dart Snippet List](https://30secondsofcode.org/dart/p/1) to see all the snippets in this project or click individual tags at the top of the same page to narrow down your search to a specific tag. 10 | * Click on each snippet card to view the whole snippet, including code, explanation and examples. 11 | * You can use the button on the right side of a snippet card to copy the code to clipboard. 12 | * If you like the project, give it a star. It means a lot to the people maintaining it. 13 | 14 | ## Want to contribute? 15 | 16 | * If you want to help us improve, take a minute to read the [Contribution Guidelines](/CONTRIBUTING.md) first. 17 | * Use the [Snippet Template](/snippet-template.md) to add new snippets to the collection. 18 | * If you find a problem with a specific snippet, please [open an issue](https://github.com/30-seconds/30-seconds-of-dart/issues/new). 19 | * If you find a problem with the website, please [report it in the web repository](https://github.com/30-seconds/30-seconds-web/issues/new). 20 | 21 | ## Credits & Sponsors 22 | 23 | * This repository is maintained by the [30-seconds organization on GitHub](https://github.com/30-seconds). 24 | * All snippets are licensed under the CC0-1.0 License, unless explicitly stated otherwise. 25 | * Logos, names and trademarks are not to be used without the explicit consent of the maintainers or owners of the 30 seconds GitHub organization. 26 | * Our website is powered by [Netlify](https://www.netlify.com/), [Gatsby](https://www.gatsbyjs.org/), [Travis CI](https://travis-ci.com/) & [GitHub](https://github.com/). 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | lerna-debug.log* 8 | 9 | # Diagnostic reports (https://nodejs.org/api/report.html) 10 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 11 | 12 | # Runtime data 13 | pids 14 | *.pid 15 | *.seed 16 | *.pid.lock 17 | 18 | # Directory for instrumented libs generated by jscoverage/JSCover 19 | lib-cov 20 | 21 | # Coverage directory used by tools like istanbul 22 | coverage 23 | *.lcov 24 | 25 | # nyc test coverage 26 | .nyc_output 27 | 28 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 29 | .grunt 30 | 31 | # Bower dependency directory (https://bower.io/) 32 | bower_components 33 | 34 | # node-waf configuration 35 | .lock-wscript 36 | 37 | # Compiled binary addons (https://nodejs.org/api/addons.html) 38 | build/Release 39 | 40 | # Dependency directories 41 | node_modules/ 42 | jspm_packages/ 43 | 44 | # TypeScript v1 declaration files 45 | typings/ 46 | 47 | # TypeScript cache 48 | *.tsbuildinfo 49 | 50 | # Optional npm cache directory 51 | .npm 52 | 53 | # Optional eslint cache 54 | .eslintcache 55 | 56 | # Microbundle cache 57 | .rpt2_cache/ 58 | .rts2_cache_cjs/ 59 | .rts2_cache_es/ 60 | .rts2_cache_umd/ 61 | 62 | # Optional REPL history 63 | .node_repl_history 64 | 65 | # Output of 'npm pack' 66 | *.tgz 67 | 68 | # Yarn Integrity file 69 | .yarn-integrity 70 | 71 | # dotenv environment variables file 72 | .env 73 | .env.test 74 | 75 | # parcel-bundler cache (https://parceljs.org/) 76 | .cache 77 | 78 | # Next.js build output 79 | .next 80 | 81 | # Nuxt.js build / generate output 82 | .nuxt 83 | dist 84 | 85 | # Gatsby files 86 | .cache/ 87 | # Comment in the public line in if your project uses Gatsby and *not* Next.js 88 | # https://nextjs.org/blog/next-9-1#public-directory-support 89 | # public 90 | 91 | # vuepress build output 92 | .vuepress/dist 93 | 94 | # Serverless directories 95 | .serverless/ 96 | 97 | # FuseBox cache 98 | .fusebox/ 99 | 100 | # DynamoDB Local files 101 | .dynamodb/ 102 | 103 | # TernJS port file 104 | .tern-port 105 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level of experience, 9 | education, socio-economic status, nationality, personal appearance, race, 10 | religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at [mst10041967@gmail.com]. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contribution Guidelines 2 | 3 | **30 seconds of code** is powered by the community, so feel free to contribute in any way you can to help us! 4 | 5 | ## Tools 6 | 7 | Before you begin contributing, you should install the integration-tools globally on your machine: 8 | 9 | ```sh 10 | npm install -g @30-seconds/integration-tools 11 | ``` 12 | 13 | This will allow you to use our customized tools for all of our content repositories. 14 | 15 | ## How you can help 16 | 17 | - Submit pull requests with new snippets (see guidelines below) or snippet updates (tags, descriptions, explanations, typos, examples, code improvements). 18 | - Open issues for things you want to see added, modified, discuss ideas or help out with existing issues. 19 | 20 | ## Ground rules 21 | 22 | Breaking any of these rules will result in your pull request being closed. Please follow these guidelines above all else: 23 | 24 | - **Always be polite and respectful to others** and try to follow the advice of the moderators/collaborators/owners. 25 | - **Only modify snippet files**, never modify the generated files in the `snippet_data` directory. 26 | - **Use the integration tools commands** to generate new snippets, ensuring they have the correct name and are in the correct location. 27 | - **Follow snippet format exactly**, otherwise your snippets will not be recognized correctly by the tools responsible for publishing them on the website. This includes such things as spacing and empty lines - if you accidentally make a mistake, consult the repository's [snippet template](snippet-template.md). 28 | - **Snippets should solve real-world problems**, no matter how simple and should be abstract enough to be applied to different scenarios. 29 | 30 | ## Snippet creation 31 | 32 | After installing the integration tools, you can run the following command: 33 | 34 | ```sh 35 | create-new-snippet 36 | ``` 37 | 38 | Replace `` with the name of the snippet you are adding. 39 | 40 | ## Snippet guidelines 41 | 42 | - Snippets must have all their frontmatter sections (title, tags etc.) filled. 43 | - Snippet titles must correspond to the filename and follow the language and repository's naming conventions. 44 | - Snippet tags must be comma-separated, contain a primary tag as seen on the website as their first tag and an expertise tag (`beginner`, `intermediate` or `advanced`) as their last tag. 45 | - Snippet descriptions must be short and to the point. Explain *what* the snippet does and detail *how* the snippet works and the language features used in it. 46 | - Snippet code and examples must be enclosed in appropriate, language-tagged blocks as shown in the snippet template, be short and use modern techniques and features. Also make sure to test your code before submitting. 47 | - If your snippet contains arguments with default parameters, explain what happens if they are omitted when calling the function and what the default case is. Specify default parameters for arguments only if necessary. 48 | - If your snippet uses recursion, use the `recursion` tag and explain the base cases. 49 | - Try to strike a balance between readability, brevity, and performance. 50 | - Always use soft tabs (2 spaces), never hard tabs. 51 | - Leave a single space after a comma (`,`) character (both in the description and code). 52 | - Define multiple variables on the same line, if possible. Use meaningful names (e.g. `letter` instead of `lt`) and follow existing conventions as seen in other snippets. Do not use trailing or leading underscores in variable names. 53 | - Always use single quotes for string literals. Use template literals, instead, if necessary. 54 | - When describing snippets, refer to methods, using their full name. For example, use `Iterable.reduce()`, instead of `reduce()`. 55 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | CC0 1.0 Universal 2 | 3 | Statement of Purpose 4 | 5 | The laws of most jurisdictions throughout the world automatically confer 6 | exclusive Copyright and Related Rights (defined below) upon the creator and 7 | subsequent owner(s) (each and all, an "owner") of an original work of 8 | authorship and/or a database (each, a "Work"). 9 | 10 | Certain owners wish to permanently relinquish those rights to a Work for the 11 | purpose of contributing to a commons of creative, cultural and scientific 12 | works ("Commons") that the public can reliably and without fear of later 13 | claims of infringement build upon, modify, incorporate in other works, reuse 14 | and redistribute as freely as possible in any form whatsoever and for any 15 | purposes, including without limitation commercial purposes. These owners may 16 | contribute to the Commons to promote the ideal of a free culture and the 17 | further production of creative, cultural and scientific works, or to gain 18 | reputation or greater distribution for their Work in part through the use and 19 | efforts of others. 20 | 21 | For these and/or other purposes and motivations, and without any expectation 22 | of additional consideration or compensation, the person associating CC0 with a 23 | Work (the "Affirmer"), to the extent that he or she is an owner of Copyright 24 | and Related Rights in the Work, voluntarily elects to apply CC0 to the Work 25 | and publicly distribute the Work under its terms, with knowledge of his or her 26 | Copyright and Related Rights in the Work and the meaning and intended legal 27 | effect of CC0 on those rights. 28 | 29 | 1. Copyright and Related Rights. A Work made available under CC0 may be 30 | protected by copyright and related or neighboring rights ("Copyright and 31 | Related Rights"). Copyright and Related Rights include, but are not limited 32 | to, the following: 33 | 34 | i. the right to reproduce, adapt, distribute, perform, display, communicate, 35 | and translate a Work; 36 | 37 | ii. moral rights retained by the original author(s) and/or performer(s); 38 | 39 | iii. publicity and privacy rights pertaining to a person's image or likeness 40 | depicted in a Work; 41 | 42 | iv. rights protecting against unfair competition in regards to a Work, 43 | subject to the limitations in paragraph 4(a), below; 44 | 45 | v. rights protecting the extraction, dissemination, use and reuse of data in 46 | a Work; 47 | 48 | vi. database rights (such as those arising under Directive 96/9/EC of the 49 | European Parliament and of the Council of 11 March 1996 on the legal 50 | protection of databases, and under any national implementation thereof, 51 | including any amended or successor version of such directive); and 52 | 53 | vii. other similar, equivalent or corresponding rights throughout the world 54 | based on applicable law or treaty, and any national implementations thereof. 55 | 56 | 2. Waiver. To the greatest extent permitted by, but not in contravention of, 57 | applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and 58 | unconditionally waives, abandons, and surrenders all of Affirmer's Copyright 59 | and Related Rights and associated claims and causes of action, whether now 60 | known or unknown (including existing as well as future claims and causes of 61 | action), in the Work (i) in all territories worldwide, (ii) for the maximum 62 | duration provided by applicable law or treaty (including future time 63 | extensions), (iii) in any current or future medium and for any number of 64 | copies, and (iv) for any purpose whatsoever, including without limitation 65 | commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes 66 | the Waiver for the benefit of each member of the public at large and to the 67 | detriment of Affirmer's heirs and successors, fully intending that such Waiver 68 | shall not be subject to revocation, rescission, cancellation, termination, or 69 | any other legal or equitable action to disrupt the quiet enjoyment of the Work 70 | by the public as contemplated by Affirmer's express Statement of Purpose. 71 | 72 | 3. Public License Fallback. Should any part of the Waiver for any reason be 73 | judged legally invalid or ineffective under applicable law, then the Waiver 74 | shall be preserved to the maximum extent permitted taking into account 75 | Affirmer's express Statement of Purpose. In addition, to the extent the Waiver 76 | is so judged Affirmer hereby grants to each affected person a royalty-free, 77 | non transferable, non sublicensable, non exclusive, irrevocable and 78 | unconditional license to exercise Affirmer's Copyright and Related Rights in 79 | the Work (i) in all territories worldwide, (ii) for the maximum duration 80 | provided by applicable law or treaty (including future time extensions), (iii) 81 | in any current or future medium and for any number of copies, and (iv) for any 82 | purpose whatsoever, including without limitation commercial, advertising or 83 | promotional purposes (the "License"). The License shall be deemed effective as 84 | of the date CC0 was applied by Affirmer to the Work. Should any part of the 85 | License for any reason be judged legally invalid or ineffective under 86 | applicable law, such partial invalidity or ineffectiveness shall not 87 | invalidate the remainder of the License, and in such case Affirmer hereby 88 | affirms that he or she will not (i) exercise any of his or her remaining 89 | Copyright and Related Rights in the Work or (ii) assert any associated claims 90 | and causes of action with respect to the Work, in either case contrary to 91 | Affirmer's express Statement of Purpose. 92 | 93 | 4. Limitations and Disclaimers. 94 | 95 | a. No trademark or patent rights held by Affirmer are waived, abandoned, 96 | surrendered, licensed or otherwise affected by this document. 97 | 98 | b. Affirmer offers the Work as-is and makes no representations or warranties 99 | of any kind concerning the Work, express, implied, statutory or otherwise, 100 | including without limitation warranties of title, merchantability, fitness 101 | for a particular purpose, non infringement, or the absence of latent or 102 | other defects, accuracy, or the present or absence of errors, whether or not 103 | discoverable, all to the greatest extent permissible under applicable law. 104 | 105 | c. Affirmer disclaims responsibility for clearing rights of other persons 106 | that may apply to the Work or any use thereof, including without limitation 107 | any person's Copyright and Related Rights in the Work. Further, Affirmer 108 | disclaims responsibility for obtaining any necessary consents, permissions 109 | or other rights required for any use of the Work. 110 | 111 | d. Affirmer understands and acknowledges that Creative Commons is not a 112 | party to this document and has no duty or obligation with respect to this 113 | CC0 or use of the Work. 114 | 115 | For more information, please see 116 | 117 | --------------------------------------------------------------------------------