axios({ url: opts === 'qotd' ? '/qotd.json' : '/quotes/', baseURL: baseUrl, headers: headers }) .then(response => { if (response.data.quotes && _.isArray(response.data.quotes)) { let result = { quote: _.sample(response.data.quotes) }; return resolve(result); } return resolve(response.data); }) .catch(err => reject(err));
net.createServer(function (c) { var worker; if (options.sticky) { var ipHash = hash((c.remoteAddress || '').split(/\./g), seed); worker = workers[ipHash % workers.length]; } else { worker = _.sample(workers); } masterDebug('Sending connection from %s to a worker %s', c.remoteAddress, worker); worker.send('sticky-session:connection', c); })
// Helper function const getRandomConnection = () => { if(self.nodes.length <= self.input_nodes.size) // use dynamic self.input_nodes.size instead throw new Error("Something went wrong. Total nodes is length is somehow less than size of inputs") return _.sample(self.connections) }
/* 生成测试数据,创建 100 个 Post, 从 User 表中随机选择用户作为 author */ AV.Cloud.define('createPostSamples', async request => { const users = await new AV.Query(AV.User).find() await AV.Object.saveAll(_.range(0, 100).map( () => { const post = new Post() post.set('author', _.sample(users)) return post })) })
suq('http://odonatagame.blogspot.com/2015/07/oh-thats-right-were-not-dead.html', function (err, data, body) { var $ = cheerio.load(body); var scraped = { title: data.meta.title || data.headers.h1[0], description: data.meta.description || $('p').text().replace(/([\r\n\t])+/ig,'').substring(0,255) +'...', images: _.sample(data.images, 8) }; console.log(scraped); });
function getDefaultScraper(config) { const { randomUserAgent } = config const requestOptions = _.cloneDeep(config.requestOptions) request.debug = config.debugRequest if (randomUserAgent) { _.set(requestOptions, 'headers["User-Agent"]', _.sample(UA.BROWSER)) } return request.defaults(requestOptions) }
function createArraySpec(predicate) { return { conform: (value, unwrap = false) => conform(value, unwrap, _.partial(_.includes, predicate)), describe: () => predicate, explain: function*(value, options) { if (!_.includes(predicate, value)) { yield getExplanation(value, options); } }, gen: () => tcg.null.then(() => tcg.return(_.sample(predicate))) }; }
function alt(...predicates) { const pairs = _.chunk(predicates, 2); return { op: 'alt', conform: ([value]) => { const found = _.find(pairs, ([, predicate]) => isValid(predicate, value)); return _.isUndefined(found) ? invalidString : [found[0], value]; }, gen: () => tcg.null.then(() => { const result = gen(_.sample(pairs)[1]); return tcg.array(result, {size: 1}); }), describe: () => [alt.name, ...describe(predicates)], explain: function*(value, {via}) { yield* explainInsufficientInput(predicates, value, via); yield* explainExtraInput(predicates, value, via); yield* explainInvalid(value, pairs, via); } }; }
myVault.prepare().bind(myVault).then(function () { if (myVault.initialized && !myVault.status.sealed) { return myVault; } return myVault.init().then(function (data) { if (data.root_token) { myVault.setToken(data.root_token); helpers.backup(data); } return myVault.unSeal({ body: { key: _.sample(_.union(data.keys || [], backupData.keys || [])) } }).then(function () { return helpers.setupVault(myVault); }); }); }).catch(function onError(err) { debuglog('(before) vault setup failed: %s', err.message); })
app.get('/author/:id', (req, res) => { const code = _.sample([200, 304, 500]); switch (code) { case 304: res.status(304).send(''); break; case 500: res.status(500).json({ error: 'The database is disconnected', }); break; default: res.json({ account: 'vicanso', }); break; } });
function simulateClientLogin() { const account = _.shuffle('abcdefghijklnmopgrstuvwxyz'.split('')).join('').substring(0, 5); const interval = _.random(0, 10 / getBase() * 120) * 1000; client.write('login') .tag({ type: _.sample(['vip', 'member', 'member', 'member']), }) .field({ account, }) .set({ RP: rp, }) .then(() => { setTimeout(simulateClientLogin, interval); }).catch(err => { console.error(err); setTimeout(simulateClientLogin, interval); }); }
author: hget(author.properties.name), url: hget(data.microformat.rels.canonical), images: _.sample(data.images, 4) };
function or(...predicates) { const pairs = _.chunk(predicates, 2); return { conform: value => { const found = _.find(pairs, ([, predicate]) => predicate(value)); return _.isUndefined(found) ? invalidString : [found[0], value]; }, unform: ([key, value]) => { const found = _.find(pairs, ([k]) => k === key); if (_.isUndefined(found)) { throw new Error(`Key ${key} does not exist in spec`); } return value; }, gen: () => _.isEmpty(predicates) ? tcg.null : tcg.null.then(() => gen(_.sample(pairs)[1])), describe: () => [or.name, ...describe(predicates)], explain: function*(value, {via}) { const problems = _.map(pairs, ([k, predicate]) => explainData(predicate, value, {path: [k], via})); if (!_.some(problems, _.isNull)) { for (let p of problems) { yield p.problems[0]; } } } }; }
app.get('/order/:id', (req, res) => { const code = _.sample([200, 304, 400, 403]); switch (code) { case 304: res.status(304).send(''); break; case 400: res.status(400).json({ error: 'The id is not valid', }); break; case 403: res.status(403).json({ error: 'Please login first', }); break; default: res.json({ account: 'vicanso', }); break; } });