const { range } = require('rxjs')
const { map, filter } = require('rxjs/operators')
range(1, 200)
.pipe(
filter(x => x % 2 === 1),
map(x => x + x)
)
.subscribe(x => console.log(x))
var validator = require('validator')
validator.isEmail('foo@bar.com') //=> true
import * as yup from 'yup'
let schema = yup.object().shape({
name: yup.string().required(),
age: yup.number().required().positive().integer(),
email: yup.string().email(),
website: yup.string().url(),
createdOn: yup.date().default(function () {
return new Date()
})
})
// check validity
schema
.isValid({
name: 'jimmy',
age: 24
})
.then(
valid => console.log(valid) // => true
)
// you can try and type cast objects to the defined schema
schema.cast({
name: 'jimmy',
age: '24',
createdOn: '2014-09-23T19:25:25Z'
})
// => { name: 'jimmy', age: 24, createdOn: Date }
import { format, formatDistance, formatRelative, subDays } from 'date-fns'
format(new Date(), '[Today is a] dddd')
//=> "Today is a Wednesday"
formatDistance(subDays(new Date(), 3), new Date())
//=> "3 days ago"
formatRelative(subDays(new Date(), 3), new Date())
//=> "last Friday at 7:26 p.m."
const { v4: uuidv4 } = require('uuid')
uuidv4() // => '1a68a438-b077-468b-b1e8-dcdd976a0f5b'
const fs = require('fs-extra')
async function copyFiles() {
try {
await fs.copy('/tmp/myfile', '/tmp/mynewfile')
console.log('success!')
} catch (err) {
console.error(err)
}
}
copyFiles()
const mkdirp = require('mkdirp')
// return value is a Promise resolving to the first directory created
mkdirp('/tmp/foo/bar/baz').then(made =>
console.log(`made directories, starting with ${made}`)
)
const glob = require('glob')
// options is optional
glob('**/*.js', options, function (er, files) {
// files is an array of filenames.
// If the `nonull` option is set, and nothing
// was found, then files is ["**/*.js"]
// er is an error object or null.
})
const shell = require('shelljs')
if (!shell.which('git')) {
shell.echo('Sorry, this script requires git')
shell.exit(1)
}
// Copy files to release dir
shell.rm('-rf', 'out/Release')
shell.cp('-R', 'stuff/', 'out/Release')
// Replace macros in each .js file
shell.cd('lib')
shell.ls('*.js').forEach(function (file) {
shell.sed('-i', 'BUILD_VERSION', 'v0.1.2', file)
shell.sed('-i', /^.*REMOVE_THIS_LINE.*$/, '', file)
shell.sed('-i', /.*REPLACE_LINE_WITH_MACRO.*\n/, shell.cat('macro.js'), file)
})
shell.cd('..')
// Run external tool synchronously
if (shell.exec('git commit -am "Auto-commit"').code !== 0) {
shell.echo('Error: Git commit failed')
shell.exit(1)
}
const yaml = require('js-yaml')
const fs = require('fs')
// Get document, or throw exception on error
try {
const doc = yaml.load(fs.readFileSync('/home/ixti/example.yml', 'utf8'))
console.log(doc)
} catch (e) {
console.log(e)
}
const winston = require('winston')
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [
// Write all logs with level `error` and below to `error.log`
new winston.transports.File({ filename: 'error.log', level: 'error' }),
// Write all logs with level `info` and below to `combined.log`
new winston.transports.File({ filename: 'combined.log' })
]
})
logger.log({
level: 'error',
message: 'Hello distributed log files!'
})
logger.info('Hello again distributed logs')
const debug = require('debug')('http'),
http = require('http'),
name = 'My App'
debug('booting %o', name)
http
.createServer(function (req, res) {
debug(req.method + ' ' + req.url)
res.end('hello\n')
})
.listen(3000, function () {
debug('listening')
})
.env file:
DB_HOST=localhost
DB_USER=root
DB_PASS=s1mpl3
require('dotenv').config();
const db = require('db');
db.connect({
host: process.env.DB_HOST,
username: process.env.DB_USER,
password: process.env.DB_PASS
});
export const real_ip = () =>
new Promise((resolve, reject) => {
let timer = null
timer ?? clearInterval(timer)
getIPs(ip => {
resolve(ip)
})
timer = setInterval(() => {
reject('时间过长')
}, 2000)
})