lodash#sample JavaScript Examples
The following examples show how to use
lodash#sample.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: products.js From course-manager with MIT License | 6 votes |
products = [...Array(24)].map((_, index) => {
const setIndex = index + 1;
return {
id: faker.datatype.uuid(),
cover: mockImgProduct(setIndex),
name: PRODUCT_NAME[index],
price: faker.datatype.number({ min: 4, max: 99, precision: 0.01 }),
priceSale: setIndex % 3 ? null : faker.datatype.number({ min: 19, max: 29, precision: 0.01 }),
colors:
(setIndex === 1 && PRODUCT_COLOR.slice(0, 2)) ||
(setIndex === 2 && PRODUCT_COLOR.slice(1, 3)) ||
(setIndex === 3 && PRODUCT_COLOR.slice(2, 4)) ||
(setIndex === 4 && PRODUCT_COLOR.slice(3, 6)) ||
(setIndex === 23 && PRODUCT_COLOR.slice(4, 6)) ||
(setIndex === 24 && PRODUCT_COLOR.slice(5, 6)) ||
PRODUCT_COLOR,
status: sample(['sale', 'new', '', ''])
};
})
Example #2
Source File: user.js From course-manager with MIT License | 6 votes |
users = [...Array(24)].map((_, index) => ({
id: faker.datatype.uuid(),
avatarUrl: mockImgAvatar(index + 1),
name: faker.name.findName(),
company: faker.company.companyName(),
isVerified: faker.datatype.boolean(),
status: sample(['active', 'banned']),
role: sample([
'Leader',
'Hr Manager',
'UI Designer',
'UX Designer',
'UI/UX Designer',
'Project Manager',
'Backend Developer',
'Full Stack Designer',
'Front End Developer',
'Full Stack Developer'
])
}))
Example #3
Source File: debug-toolbar.js From ThreatMapper with Apache License 2.0 | 6 votes |
setIntermittent() {
// simulate epheremal nodes
if (this.intermittentTimer) {
clearInterval(this.intermittentTimer);
this.intermittentTimer = null;
} else {
this.intermittentTimer = setInterval(() => {
// add new node
this.addNodes(1);
// remove random node
const ns = this.props.nodes;
const nodeNames = ns.keySeq().toJS();
const randomNode = sample(nodeNames);
this.asyncDispatch(receiveNodesDelta({
remove: [randomNode]
}));
}, 1000);
}
}
Example #4
Source File: debug-toolbar.js From ThreatMapper with Apache License 2.0 | 6 votes |
setShortLived() {
// simulate nodes with same ID popping in and out
if (this.shortLivedTimer) {
clearInterval(this.shortLivedTimer);
this.shortLivedTimer = null;
} else {
this.shortLivedTimer = setInterval(() => {
// filter random node
const ns = this.props.nodes;
const nodeNames = ns.keySeq().toJS();
const randomNode = sample(nodeNames);
if (randomNode) {
let nextNodes = ns.setIn([randomNode, 'filtered'], true);
this.shortLivedNodes = this.shortLivedNodes.add(randomNode);
// bring nodes back after a bit
if (this.shortLivedNodes.size > 5) {
const returningNode = this.shortLivedNodes.first();
this.shortLivedNodes = this.shortLivedNodes.rest();
nextNodes = nextNodes.setIn([returningNode, 'filtered'], false);
}
this.asyncDispatch(setAppState(state => state.set('nodes', nextNodes)));
}
}, 1000);
}
}
Example #5
Source File: debug-toolbar.js From ThreatMapper with Apache License 2.0 | 6 votes |
createRandomNodes(n, prefix = 'zing') {
const ns = this.props.nodes;
const nodeNames = ns.keySeq().toJS();
const newNodeNames = range(ns.size, ns.size + n).map(i => (
// `${randomLetter()}${randomLetter()}-zing`
`${prefix}${i}`
));
const allNodes = nodeNames.concat(newNodeNames);
return newNodeNames.map(name => deltaAdd(
name,
sampleArray(allNodes),
sample(SHAPES),
sample(STACK_VARIANTS),
sampleArray(NETWORKS, 10)
));
}
Example #6
Source File: user.js From Django-REST-Framework-React-BoilerPlate with MIT License | 6 votes |
users = [...Array(24)].map((_, index) => ({
id: faker.datatype.uuid(),
avatarUrl: `/static/mock-images/avatars/avatar_${index + 1}.jpg`,
name: faker.name.findName(),
company: faker.company.companyName(),
isVerified: faker.datatype.boolean(),
status: sample(['active', 'banned']),
role: sample([
'Leader',
'Hr Manager',
'UI Designer',
'UX Designer',
'UI/UX Designer',
'Project Manager',
'Backend Developer',
'Full Stack Designer',
'Front End Developer',
'Full Stack Developer',
]),
}))
Example #7
Source File: loading.js From ThreatMapper with Apache License 2.0 | 5 votes |
constructor(props, context) {
super(props, context);
this.state = {
template: sample(LOADING_TEMPLATES)
};
}
Example #8
Source File: User.js From gutenberg-forms with GNU General Public License v2.0 | 4 votes |
useUserStore = create(
persist(
(set, get) => ({
_hasHydrated: false,
firstLoadedOn: new Date().toISOString(),
email: '',
apiKey: '',
uuid: '',
sdkPartner: '',
noticesDismissedAt: {},
modalNoticesDismissedAt: {},
imports: 0, // total imports over time
runningImports: 0, // timed imports, resets to 0 every month
allowedImports: 0, // Max imports the Extendify service allows
freebieImports: 0, // Various free imports from actions (rewards)
entryPoint: 'not-set',
enabled: isGlobalLibraryEnabled(),
canInstallPlugins: false,
canActivatePlugins: false,
participatingTestsGroups: {},
preferredOptions: {
taxonomies: {},
type: '',
search: '',
},
incrementImports: () => {
// If the user has freebie imports, use those first
const freebieImports =
Number(get().freebieImports) > 0
? Number(get().freebieImports) - 1
: Number(get().freebieImports)
// If they don't, then increment the running imports
const runningImports =
Number(get().runningImports) + +(freebieImports < 1)
set({
imports: Number(get().imports) + 1,
runningImports,
freebieImports,
})
},
giveFreebieImports: (amount) => {
set({ freebieImports: get().freebieImports + amount })
},
totalAvailableImports: () => {
return (
Number(get().allowedImports) + Number(get().freebieImports)
)
},
testGroup(testKey, groupOptions) {
if (!Object.keys(activeTests).includes(testKey)) return
let groups = get().participatingTestsGroups
// If the test is already in the group, don't add it again
if (!groups[testKey]) {
set({
participatingTestsGroups: Object.assign({}, groups, {
[testKey]: sample(groupOptions),
}),
})
}
groups = get().participatingTestsGroups
return groups[testKey]
},
activeTestGroups() {
return Object.entries(get().participatingTestsGroups)
.filter(([key]) => Object.keys(activeTests).includes(key))
.reduce((obj, [key, value]) => {
obj[key] = value
return obj
}, {})
},
activeTestGroupsUtmValue() {
const active = Object.entries(get().activeTestGroups())
.map(([key, value]) => {
return `${activeTests[key]}=${value}`
}, '')
.join(':')
return encodeURIComponent(active)
},
hasAvailableImports: () => {
return get().apiKey
? true
: Number(get().runningImports) <
Number(get().totalAvailableImports())
},
remainingImports: () => {
const remaining =
Number(get().totalAvailableImports()) -
Number(get().runningImports)
// If they have no allowed imports, this might be a first load
// where it's just fetching templates (and/or their max alllowed)
if (!get().allowedImports) {
return null
}
return remaining > 0 ? remaining : 0
},
updatePreferredSiteType: (value) => {
get().updatePreferredOption('siteType', value)
},
updatePreferredOption: (option, value) => {
// If the option doesn't exist, assume it's a taxonomy
if (
!Object.prototype.hasOwnProperty.call(
get().preferredOptions,
option,
)
) {
value = Object.assign(
{},
get().preferredOptions?.taxonomies ?? {},
{ [option]: value },
)
option = 'taxonomies'
}
set({
preferredOptions: {
...Object.assign({}, get().preferredOptions, {
[option]: value,
}),
},
})
},
// Will mark a modal or footer notice
markNoticeSeen: (key, type) => {
set({
[`${type}DismissedAt`]: {
...get()[`${type}DismissedAt`],
[key]: new Date().toISOString(),
},
})
},
}),
{
name: 'extendify-user',
getStorage: () => storage,
onRehydrateStorage: () => () => {
useUserStore.setState({ _hasHydrated: true })
},
partialize: (state) => {
delete state._hasHydrated
return state
},
},
),
)