|
| 1 | +// Copyright (c) 2016, Damon Douglas. All rights reserved. Use of this source code |
| 2 | +// is governed by a BSD-style license that can be found in the LICENSE file. |
| 3 | + |
| 4 | +/// Simple library for generating random ascii strings. |
| 5 | +/// |
| 6 | +/// More dartdocs go here. |
| 7 | +/// |
| 8 | +/// |
| 9 | +/// A simple usage example: |
| 10 | +/// |
| 11 | +/// import 'package:random_string/random_string.dart' as random; |
| 12 | +/// main() { |
| 13 | +/// print(randomBetween(10,20)); // some integer between 10 and 20 |
| 14 | +/// print(randomNumeric(4)); // sequence of 4 random numbers i.e. 3259 |
| 15 | +/// print(randomString(10)); // random sequence of 10 characters i.e. e~f93(4l- |
| 16 | +/// print(randomAlpha(5)); // random sequence of 5 alpha characters i.e. aRztC |
| 17 | +/// print(randomAlphaNumeric(10)); // random sequence of 10 alpha numeric i.e. aRztC1y32B |
| 18 | +/// } |
| 19 | +
|
| 20 | +library random_string; |
| 21 | + |
| 22 | +import 'dart:math'; |
| 23 | + |
| 24 | +const ASCII_START = 33; |
| 25 | +const ASCII_END = 126; |
| 26 | +const NUMERIC_START = 48; |
| 27 | +const NUMERIC_END = 57; |
| 28 | +const LOWER_ALPHA_START = 97; |
| 29 | +const LOWER_ALPHA_END = 122; |
| 30 | +const UPPER_ALPHA_START = 65; |
| 31 | +const UPPER_ALPHA_END = 90; |
| 32 | + |
| 33 | +/// Generates a random integer where [from] <= [to]. |
| 34 | +int randomBetween(int from, int to) { |
| 35 | + if (from > to) throw new Exception('$from cannot be > $to'); |
| 36 | + var rand = new Random(); |
| 37 | + return ((to - from) * rand.nextDouble()).toInt() + from; |
| 38 | +} |
| 39 | + |
| 40 | +/// Generates a random string of [length] with characters |
| 41 | +/// between ascii [from] to [to]. |
| 42 | +/// Defaults to characters of ascii '!' to '~'. |
| 43 | +String randomString(int length, {int from: ASCII_START, int to: ASCII_END}) { |
| 44 | + return new String.fromCharCodes( |
| 45 | + new List.generate(length, (index) => randomBetween(from, to))); |
| 46 | +} |
| 47 | + |
| 48 | +/// Generates a random string of [length] with only numeric characters. |
| 49 | +String randomNumeric(int length) => |
| 50 | + randomString(length, from: NUMERIC_START, to: NUMERIC_END); |
| 51 | +/* |
| 52 | +/// Generates a random string of [length] with only alpha characters. |
| 53 | +String randomAlpha(int length) { |
| 54 | + var lowerAlphaLength = randomBetween(0, length); |
| 55 | + var upperAlphaLength = length - lowerAlphaLength; |
| 56 | + var lowerAlpha = randomString(lowerAlphaLength, |
| 57 | + from: LOWER_ALPHA_START, to: LOWER_ALPHA_END); |
| 58 | + var upperAlpha = randomString(upperAlphaLength, |
| 59 | + from: UPPER_ALPHA_START, to: UPPER_ALPHA_END); |
| 60 | + return randomMerge(lowerAlpha, upperAlpha); |
| 61 | +} |
| 62 | +
|
| 63 | +/// Generates a random string of [length] with alpha-numeric characters. |
| 64 | +String randomAlphaNumeric(int length) { |
| 65 | + var alphaLength = randomBetween(0, length); |
| 66 | + var numericLength = length - alphaLength; |
| 67 | + var alpha = randomAlpha(alphaLength); |
| 68 | + var numeric = randomNumeric(numericLength); |
| 69 | + return randomMerge(alpha, numeric); |
| 70 | +} |
| 71 | +
|
| 72 | +/// Merge [a] with [b] and scramble characters. |
| 73 | +String randomMerge(String a, String b) { |
| 74 | + var mergedCodeUnits = new List.from("$a$b".codeUnits); |
| 75 | + mergedCodeUnits.shuffle(); |
| 76 | + return new String.fromCharCodes(mergedCodeUnits); |
| 77 | +}*/ |
0 commit comments