本文整理汇总了TypeScript中randomstring.generate函数的典型用法代码示例。如果您正苦于以下问题:TypeScript generate函数的具体用法?TypeScript generate怎么用?TypeScript generate使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了generate函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: test
test('user', async (x) => {
let usermodel: any = models.User;
//record the count
let oldcount = (await usermodel.model.$count({ filter: { _status: 1 } })).data.count;
let newrecords = [];
// create 10 records
for (let i = 0; i < 10; i++) {
newrecords.push(await usermodel.model.$create({
name: randomstring.generate(5),
password: randomstring.generate(9),
email: randomstring.generate(6) + '@qq.com',
phone: randomstring.generate({
length: 11,
charset: 'numeric'
})
}));
}
let curcount = (await usermodel.model.$count({ filter: { _status: 1 } })).data.count;
//if 10 records have been created
x.is(curcount, oldcount + 10);
console.log('10 records have been created');
//delete them
let dellist = newrecords.map(x => x.data._id);
let delresult = await usermodel.model.$delete(dellist);
curcount = (await usermodel.model.$count({ filter: { _status: 1 } })).data.count;
x.is(curcount, oldcount);
console.log('10 records have been deleted');
});
示例2: test
test('user', async (x) => {
//get usergroup
let usergroupmodel: any = models.UserGroup;
let usergroupdef = (await usergroupmodel.model.$paginate({ filter: { name: 'test' } })).data;
usergroupdef = usergroupdef ? usergroupdef.list[0] : {};
console.log('test usergroup');
console.log(JSON.stringify(usergroupdef, null, 4));
let usermodel: any = models.User;
//record the count
let countdata = (await usermodel.model.$count({ filter: { _status: 1 } }));
let oldcount = countdata.data.count;
let newrecords = [];
// create 10 records
for (let i = 0; i < 10; i++) {
newrecords.push(await usermodel.model.$create({
name: randomstring.generate(5),
password: randomstring.generate(9),
email_value: randomstring.generate(6) + '@qq.com',
phone_value: randomstring.generate({
length: 11,
charset: 'numeric'
}),
role: usergroupdef._id
}));
}
let curcountres = (await usermodel.model.$count({ filter: { _status: 1 } }));
console.log(curcountres);
let curcount = curcountres.data.count;
//if 10 records have been created
x.is(curcount, oldcount + 10);
console.log('10 records have been created');
//update
for (let i = 0; i < newrecords.length; i++) {
let tt = newrecords[i];
let uptbody = {
id: tt.data._id,
data: {
name: tt.data.name + 'changed'
}
};
//console.log(tt);
//console.log(uptbody);
let uptresult = await usermodel.model.$update(uptbody);
//console.log(uptresult);
x.is(uptresult.data.name, tt.data.name + 'changed');
}
console.log('update finished');
//pagination
let pageres = await usermodel.model.$paginate({ limit:10});
console.log(pageres);
//delete them
let dellist = newrecords.map(x => x.data._id);
let delresult = await usermodel.model.$delete(dellist);
curcount = (await usermodel.model.$count({ filter: { _status: 1 } })).data.count;
x.is(curcount, oldcount);
console.log('10 records have been deleted');
});
示例3: test
test('note', async (x) => {
let s_model: any = models.Note;
//record the count
let countdata = (await s_model.model.$count({ filter: { _status: 1 } }));
console.log(countdata);
let oldcount = countdata.data.count;
let newrecords = [];
//get one category
let catamodel: any = models.Category;
let catares = await catamodel.model.$paginate({ limit: 1 });
//get two media
let mediamodel: any = models.Media;
let mediares = await mediamodel.model.$paginate({ limit: 2 });
let mediaarr = mediares.data.list.map(x => x._id);
console.log(mediaarr);
// create 10 records
for (let i = 0; i < 10; i++) {
newrecords.push(await s_model.model.$create({
name: randomstring.generate(7),
category: (catares.data.list)[0]._id,
media: mediaarr.join(','),
txt: randomstring.generate(40)
}));
}
let curcountres = (await s_model.model.$count({ filter: { _status: 1 } }));
//console.log(JSON.stringify(newrecords,null,4));
let curcount = curcountres.data.count;
//if 10 records have been created
// x.is(curcount, oldcount + 10);
console.log('10 records have been created');
//update
for (let i = 0; i < newrecords.length; i++) {
let tt = newrecords[i];
let uptbody = {
id: tt.data._id,
data: {
name: tt.data.name + 'changed'
}
};
//console.log(tt);
//console.log(uptbody);
let uptresult = await s_model.model.$update(uptbody);
//console.log(uptresult);
x.is(uptresult.data.name, tt.data.name + 'changed');
}
console.log('update finished');
//pagination
let pageres = await s_model.model.$paginate({ limit:10});
//console.log(JSON.stringify(pageres,null,4));
let dellist = newrecords.map(x => x.data._id);
let delresult = await s_model.model.$delete(dellist);
curcount = (await s_model.model.$count({ filter: { _status: 1 } })).data.count;
x.is(curcount, oldcount);
console.log('10 records have been deleted');
});
示例4: test
test('mediaform', async (x) => {
let s_model: any = models.MediaForm;
//record the count
let countdata = (await s_model.model.$count({ filter: { _status: 1 } }));
let oldcount = countdata.data.count;
console.log(`Before all tests,the count is ${oldcount}`);
let newrecords = [];
// create 10 records
for (let i = 0; i < 10; i++) {
newrecords.push(await s_model.model.$create({
name: randomstring.generate(7),
width: 200,
height: 300,
code: randomstring.generate({
length: 5,
charset: 'numeric'
}),
needToUpload:false
}));
}
//console.log(newrecords)
let curcountres = (await s_model.model.$count({ filter: { _status: 1 } }));
//console.log(curcountres);
let curcount = curcountres.data.count;
//if 10 records have been created
x.is(curcount, oldcount + 10);
console.log('10 records have been created');
//update
for (let i = 0; i < newrecords.length; i++) {
let tt = newrecords[i];
let uptbody = {
id: tt.data._id,
data: {
name: tt.data.name + 'changed'
}
};
//console.log(tt);
//console.log(uptbody);
let uptresult = await s_model.model.$update(uptbody);
//console.log(uptresult);
x.is(uptresult.data.name, tt.data.name + 'changed');
}
console.log('update finished');
//pagination
let pageres = await s_model.model.$paginate({ limit:10});
console.log(pageres);
//delete them
let dellist = newrecords.map(x => x.data._id);
let delresult = await s_model.model.$delete(dellist);
curcount = (await s_model.model.$count({ filter: { _status: 1 } })).data.count;
x.is(curcount, oldcount);
console.log('10 records have been deleted');
});
示例5: test
test('Category', async (x) => {
let s_model: any = models.Category;
//record the count
let countdata = (await s_model.model.$count({ filter: { _status: 1 } }));
let oldcount = countdata.data.count;
let newrecords = [];
//get one cover
let covermodel: any = models.Media;
let coverres = await covermodel.model.$paginate({ limit: 1 });
//get one user
let usermodel:any = models.User;
let userres = await usermodel.model.$paginate({ limit: 1 });
// console.log(coverres);
// create 10 records
for (let i = 0; i < 10; i++) {
newrecords.push(await s_model.model.$create({
name: randomstring.generate(7),
user: (userres.data.list)[0]._id,
cover: (coverres.data.list)[0]._id,
path: randomstring.generate(6) + '.jpg',
}));
}
let curcountres = (await s_model.model.$count({ filter: { _status: 1 } }));
//console.log(curcountres);
let curcount = curcountres.data.count;
//if 10 records have been created
x.is(curcount, oldcount + 10);
console.log('10 records have been created');
//update
for (let i = 0; i < newrecords.length; i++) {
let tt = newrecords[i];
let uptbody = {
id: tt.data._id,
data: {
name: tt.data.name + 'changed'
}
};
//console.log(tt);
//console.log(uptbody);
let uptresult = await s_model.model.$update(uptbody);
//console.log(uptresult);
x.is(uptresult.data.name, tt.data.name + 'changed');
}
console.log('update finished');
//pagination
let pageres = await s_model.model.$paginate({ limit:10});
//console.log(pageres);
//delete them
let dellist = newrecords.map(x => x.data._id);
let delresult = await s_model.model.$delete(dellist);
curcount = (await s_model.model.$count({ filter: { _status: 1 } })).data.count;
x.is(curcount, oldcount);
console.log('10 records have been deleted');
});
示例6: test
test('Media', async (x) => {
let s_model: any = models.Media;
//record the count
let countdata = (await s_model.model.$count({ filter: { _status: 1 } }));
let oldcount = countdata.data.count;
console.log(`Before all tests,the count is ${oldcount}`);
let newrecords = [];
//get one MediaForm
let i_model:any = models.MediaForm;
let mediaform = await i_model.model.$paginate({ limit: 1 });
console.log(JSON.stringify(mediaform,null,4));
// create 10 records
for (let i = 0; i < 10; i++) {
newrecords.push(await s_model.model.$create({
name: randomstring.generate(6),
form: (mediaform.data.list)[0]._id,
path: randomstring.generate(10)
}));
}
let curcountres = (await s_model.model.$count({ filter: { _status: 1 } }));
//console.log(curcountres);
let curcount = curcountres.data.count;
//if 10 records have been created
x.is(curcount, oldcount + 10);
console.log('10 records have been created');
//update
for (let i = 0; i < newrecords.length; i++) {
let tt = newrecords[i];
let uptbody = {
id: tt.data._id,
data: {
name: tt.data.name + 'changed'
}
};
//console.log(tt);
//console.log(uptbody);
let uptresult = await s_model.model.$update(uptbody);
//console.log(uptresult);
x.is(uptresult.data.name, tt.data.name + 'changed');
}
console.log('update finished');
//pagination
let pageres = await s_model.model.$paginate({ limit:10});
//console.log(JSON.stringify(pageres,null,4));
//delete them
let dellist = newrecords.map(x => x.data._id);
let delresult = await s_model.model.$delete(dellist);
curcount = (await s_model.model.$count({ filter: { _status: 1 } })).data.count;
x.is(curcount, oldcount);
console.log('10 records have been deleted');
});
示例7: test
test('usergroup', async (x) => {
let s_model: any = models.UserGroup;
//record the count
let countdata = (await s_model.model.$count({ filter: { _status: 1 } }));
let oldcount = countdata.data.count;
let newrecords = [];
// create 10 records
for (let i = 0; i < 10; i++) {
newrecords.push(await s_model.model.$create({
name: randomstring.generate(7),
code: randomstring.generate({
length: 6,
charset: 'numeric'
})
}));
}
let curcountres = (await s_model.model.$count({ filter: { _status: 1 } }));
// console.log(JSON.stringify(newrecords,null,4));
let curcount = curcountres.data.count;
//if 10 records have been created
x.is(curcount, oldcount + 10);
console.log('10 records have been created');
//update
for (let i = 0; i < newrecords.length; i++) {
let tt = newrecords[i];
let uptbody = {
id: tt.data._id,
data: {
name: tt.data.name + 'changed'
}
};
//console.log(tt);
//console.log(uptbody);
let uptresult = await s_model.model.$update(uptbody);
//console.log(uptresult);
x.is(uptresult.data.name, tt.data.name + 'changed');
}
console.log('update finished');
//pagination
let pageres = await s_model.model.$paginate({ limit:10});
//console.log(pageres);
//delete them
let dellist = newrecords.map(x => x.data._id);
let delresult = await s_model.model.$delete(dellist);
curcount = (await s_model.model.$count({ filter: { _status: 1 } })).data.count;
x.is(curcount, oldcount);
console.log('10 records have been deleted');
});
示例8: callback
user.repository.findOne({ username: username }, (err, u) => {
if (err || !u) {
return callback('Username not found');
}
var newPass = randomstring.generate(8);
u.password = createHash(newPass);
u.save();
var subject = replaceAll(config.forgotTemplate.subject, '{{username}}', u.username);
var text = replaceAll(config.forgotTemplate.text, '{{username}}', u.username);
var html = replaceAll(config.forgotTemplate.html, '{{username}}', u.username);
subject = replaceAll(subject, '{{password}}', newPass);
text = replaceAll(text, '{{password}}', newPass);
html = replaceAll(html, '{{password}}', newPass);
mailerAPI.sendMail({
from: config.forgotTemplate.sender, // sender address
to: u.username, // list of receivers
subject: subject, // Subject line
text: text, // plaintext body
html: html // html body
}, (err, info) => {
if (err) {
logger.error("Error sending new password email", err);
}
callback('Email sent!');
});
});
示例9: createUser
public static async createUser(ctx: Context, userObj: UserDocument, confirmUserEmail: boolean): Promise<UserDocument> {
let user = new state.models.User(assign(userObj, {
created_at: new Date(),
roles: ['member'],
_plan: config.vpdb.quota.defaultPlan,
}));
if (confirmUserEmail) {
user.email_status = {
code: 'pending_registration',
token: randomString.generate(16),
expires_at: new Date(new Date().getTime() + 86400000), // 1d valid
value: userObj.email,
};
} else {
user.email_status = { code: 'confirmed' };
user.is_active = true;
user.validated_emails = [userObj.email];
}
await user.validate();
const count = await state.models.User.countDocuments({}).exec();
user.roles = count ? ['member'] : ['root'];
user = await user.save();
await acl.addUserRoles(user.id, user.roles);
logger.info(ctx.state, '[UserUtil.createUser] %s <%s> successfully created with ID "%s" and plan "%s".', count ? 'User' : 'Root user', user.email, user.id, user._plan);
return user;
}
示例10: it
it('should reject how much owed text with more than max allowed characters', () => {
const text = randomstring.generate({
length: ValidationConstraints.FREE_TEXT_MAX_LENGTH + 1,
charset: 'alphabetic'
})
const errors = validator.validateSync(new HowMuchOwed(300, text))
expect(errors.length).to.equal(1)
expectValidationError(errors, ValidationErrors.TEXT_TOO_LONG)
})