Skip to content

randPrefix

Prepends a random token to a string, or to every string in an array. It mirrors randSuffix, for the places where the distinguishing part belongs in front: a shard, a tenant, a key that sorts by nothing in particular. The token is the same one, and so is what it is not for.

javascript
import { randNickname, randPrefix } from 'randino';

randPrefix('MistyOwl'); // 'nVtRC_MistyOwl'
randPrefix('order-4021', { length: 4, separator: '-' }); // 'k3Rm-order-4021'

randPrefix(randNickname({ language: 'en', count: 2 }));
// ['AVcCV_MistyOwl', 'RUKAP_RustyBoot']
OptionTypeDefaultDescription
valuestring | string[]What to prepend to. The first argument, not an option. Omit it for the bare token
lengthnumber5Characters in the token. Clamped to 132
separatorstring'_'Placed between the token and the value
charsetstringbuilt-inCharacters the token is drawn from
random() => numberWhere the randomness comes from — see Choosing the source

Returns a string for a string, and a string[] for a string[].

dart
import 'package:randino/randino.dart';

randPrefix(value: 'MistyOwl'); // 'nVtRC_MistyOwl'
randPrefix(value: 'order-4021', length: 4, separator: '-'); // 'k3Rm-order-4021'

randPrefixAll(randNickname(language: WordLanguage.en, count: 2));
// ['AVcCV_MistyOwl', 'RUKAP_RustyBoot']
ParameterTypeDefaultDescription
valueString?nullWhat to prepend to. Omit it for the bare token
lengthint5Characters in the token. Clamped to 132
separatorString'_'Placed between the token and the value
charsetString?nullCharacters the token is drawn from
randomRandom?nullWhere the randomness comes from — see Choosing the source

Returns a String. randPrefixAll is the list form, for the same reason randSuffixAll is.

python
from randino import rand_nickname, rand_prefix

rand_prefix("MistyOwl")  # 'nVtRC_MistyOwl'
rand_prefix("order-4021", length=4, separator="-")  # 'k3Rm-order-4021'

rand_prefix(rand_nickname(language="en", count=2))
# ['AVcCV_MistyOwl', 'RUKAP_RustyBoot']
ArgumentTypeDefaultDescription
valuestr | list[str] | NoneNoneWhat to prepend to. Positional; the rest are keyword-only. Omit it for the bare token
lengthint5Characters in the token. Clamped to 132
separatorstr"_"Placed between the token and the value
charsetstr""Characters the token is drawn from; empty means the default
randomCallable[[], float] | NoneNoneWhere the randomness comes from — see Choosing the source

Returns a str for a str, and a list[str] for a list[str].

Everything else is randSuffix

The token, the defaults, the clamping and the fresh-token-per-value rule are the same. The two share their implementation and differ by which side the token lands on. That includes the value being optional: randPrefix() hands back the same bare token randSuffix() does. What randSuffix says about the charset applies here unchanged.

See also

  • randSuffix — the same token, behind instead of in front.
  • randModifier — the third decorator, which attaches a word rather than a token.
  • Constants — the default charset, and the length bound.

Released under the MIT License