How to use the first function from chance

Find comprehensive JavaScript chance.first code examples handpicked from public code repositorys.

111
112
113
114
115
116
117
118
119
120
 * name('prefix', {gender: 'male', middle: 'full'});
 */
APIRandom.name = function () {
  var opts = helpers.mix(arguments);
  var g = opts.gender ? {gender: opts.gender} : {};
  if (opts.first) return chance.first(g);
  if (opts.last) return chance.last(g);
  var m = {}, p = {}, prefix = '', name;
  if (opts.middle)
    m = opts.middle === 'full' ? {middle: true} : {middle_initial: true};
fork icon12
star icon33
watch icon3

34
35
36
37
38
39
40
41
42
43
const bday = Chance.birthday();

// basic demographics
const person = {
  names: [{
    givenName: Chance.first({
      gender,
    }),
    familyName: Chance.last({
      gender,
fork icon8
star icon16
watch icon5

+ 9 other calls in file

18
19
20
21
22
23
24
25
26
27
  return db.run(tableQuery)
})
.then(() => {
  const lastNames = ['Doggo', 'Pupster', 'Smith', 'Wolf']
  const puppies = longArray(100).map(() => {
    const first = chance.first()
    const last = lastNames[Math.floor(Math.random() * 4)]
    const ssn = chance.ssn()
    const callsign = chance.radio()
    const age = Math.floor(chance.age() / 7)
fork icon0
star icon1
watch icon1

+ 5 other calls in file

129
130
131
132
133
134
135
136
137
138

function generateList(limit = 100){
  var arr = new Array(limit)

  for(var i = 0; i < arr.length; i++){
    var first = chance.first(), last = chance.last()
    arr[i] = { id: i + 1, name: `${first} ${last}`, first, surname: last }
  }

  return arr
fork icon407
star icon0
watch icon5

+ 5 other calls in file

25
26
27
28
29
30
31
32
33
34
  return results;
}

function randUser () {
  return User.build({
    firstName: chance.first(),
    lastName: chance.last(),
    isAdmin: false,
    email: emails.pop(),
    gender: chance.gender(),
fork icon3
star icon12
watch icon6

+ 3 other calls in file

140
141
142
143
144
145
146
147
148
149
    break;
case 'email':
    s += chance.email().toCsv(CSV.delimiter);
    break;
case 'first':
    s += chance.first().toCsv(CSV.delimiter);
    break;
case 'float':
    s += chance.floating();
    break;
fork icon2
star icon8
watch icon0

54
55
56
57
58
59
60
61
62
    }
},

student: (options = {}) => ({
    id: random.integer({ min: 10000000, max: 999999999 }).toString(),
    name: random.first(),
    email: random.email(),
    ...options
}),
fork icon4
star icon3
watch icon0

36
37
38
39
40
41
42
43
44
45
it('The user can fetch his profile with getMyProfile', () => {
  cy.gql({token, query: getMyProfile}).should(spok({id, ...matchProfile}))
})

it('The user can edit their profile with editMyProfile', () => {
  const newName = chance.first()

  cy.gql({
    token,
    query: editMyProfile,
fork icon0
star icon3
watch icon0

11
12
13
14
15
16
17
18
19
20
exports.integer = function (s, e) { return ch.integer({min: s, max: e}) }
exports.random = function(options) {
    return ch.pick(options)
}
exports.pick = exports.random
exports.firstName = function () { return ch.first() }
exports.lastName = function () { return ch.last() }
exports.gender = function () { return ch.gender() }
exports.company = function () { return ch.capitalize(ch.word({syllables: 3})) }
exports.email = function () { return ch.email() }
fork icon0
star icon2
watch icon1

+ 3 other calls in file

54
55
56
57
58
59
60
61
62
63
64
65
    });
}


function randArtist() {
    return {
        name: chance.first() + ' ' + chance.last()
    };
}


function randReview() {
fork icon1
star icon1
watch icon0

165
166
167
168
169
170
171
172
173
174
    }));
},
function (cb) {
    async.forEach(_.range(1, userCount + 1), function (nr, cb) {
        models.User.create({
            "firstName": chance.first(),
            "lastName": chance.last(),
            "email": "user" + nr + "@domain.com",
            "email_lowered": "user" + nr + "@domain.com",
            "passwordHash": defaultPasswordHash,
fork icon0
star icon1
watch icon0

+ 3 other calls in file

5
6
7
8
9
10
11
12
13
14
15
16


const a_random_user = async role => {
  const cognito = new AWS.CognitoIdentityServiceProvider();
  const clientId = process.env.WEB_COGNITO_USER_POOL_CLIENT_ID;


  const firstName = chance.first({ nationality: 'en' });
  const lastName = chance.first({ nationality: 'en' });
  const suffix = chance.string({
    length: 4,
    pool: 'abcdefghijklmnopqrstuvwxyz',
fork icon0
star icon1
watch icon0

15
16
17
18
19
20
21
22
23
24
const gender = chance.gender().toLowerCase();

const person = {
    type: spec.label,
    prefix: chance.prefix({gender: gender }),
    first: chance.first({gender: gender}),
    last: chance.last(),
    age: chance.age(),
    birthday: chance.birthday({string: true, american: true}),
    gender: gender,
fork icon0
star icon1
watch icon0

10
11
12
13
14
15
16
17
18
19
20
const anAuthenticatedUser = async () => {
    const client = new CognitoIdentityProviderClient({region: 'us-east-1'});
    const userpoolId = process.env.cognito_user_pool_id;
    const clientId = process.env.cognito_server_client_id;


    const firstName = chance.first({nationality: 'en'});
    const lastName = chance.last({nationality: 'en'});
    const suffix = chance.string({length: 4, pool: 'abcdefghijklmnopqrstuvwxyz'});
    const username = `test-${firstName}-${lastName}-${suffix}`;
    const password = randomPassword();
fork icon0
star icon0
watch icon0

7
8
9
10
11
12
13
14
15
16
const cognito = new AWS.CognitoIdentityServiceProvider()

const userpoolId = process.env.cognito_user_pool_id
const clientId = process.env.cognito_server_client_id

const firstName = chance.first({ nationality: "en" })
const lastName  = chance.last({ nationality: "en" })
const suffix    = chance.string({length: 8, pool: "abcdefghijklmnopqrstuvwxyz"})
const username  = `test-${firstName}-${lastName}-${suffix}`
const password  = random_password()
fork icon0
star icon0
watch icon0

23
24
25
26
27
28
29
30
31
32
33
34
	return chance.email(options)
}


// first
export function first (options) {
	return chance.first(options)
}


// floating
export function floating (options) {
fork icon0
star icon0
watch icon0

15
16
17
18
19
20
21
22
23
24
25
const workerPort = 3000;


// create a JSON info object with some application information 
const info = {
  launchDate: new Date().toLocaleString("en-US", { timeZone: "America/Chicago" }),
  serverName: chance.first() + " the " + chance.animal(),
  appName: package.name,
  serverVersion: package.version
};

fork icon0
star icon0
watch icon0

77
78
79
80
81
82
83
84
85
86
// validate data
if(request.body.gender && request.body.country && request.body.regid) {    

 // store data in mongoDB; assign a username
 var newID = uuid.v4()
 var newName = chance.first() + chance.integer({min:1,max:1000});
 var newUser = new User({
 	user_id: newID,
 	username: newName,
 	regid: request.body.regid,
fork icon0
star icon0
watch icon0

+ 2 other calls in file

29
30
31
32
33
34
35
36
37
38
39
40
}


function playRoute(routeFile) {
    const route = fs.readFileSync(routeFile, 'utf8');
    const coords = route.split(/\r?\n/);
    const orderId = chance.first();


    let counter = 0;
    const timer = setInterval(() => {
        const line = coords[counter];
fork icon0
star icon0
watch icon0