我试图将包含字符串,数字和数字的数组排序为字符串(例如'1','2').我想对这个数组进行排序,以便排序的数组首先包含数字,然后是包含数字的字符串,最后是字符串.
var arr = [9,5,'2','ab','3',-1 ] // to be sorted
arr.sort()
// arr = [-1, 5, 9, "2", "3","ab"] // expected result
//arr = [-1, "2", 5, 9, "ab"] // actual result
Run Code Online (Sandbox Code Playgroud)
我也试过了
var number =[];
var char =[];
arr.forEach(a=>{
if(typeof a == 'number') number.push(a);
else char.push(a);
})
arr = (number.sort((a,b)=> a>b)).concat(char.sort((a,b)=> a>b))
// arr = [-1, 5, 9, "2", "3","ab"] // expected result
// arr = [-1, 5, 9, "2", "ab", "3"]// actual result
Run Code Online (Sandbox Code Playgroud) 我正在使用 ssh2-sftp-client 从远程服务器获取文件。我正在将文件放入可读流中。我想将此可读流转换为所需的文件(sample.png 作为 png 文件,sample.doc 文件作为 doc 文件等)
\n\n这是我的代码-
\n\nlet Client = require(\'ssh2-sftp-client\');\nlet sftp = new Client();\n sftp.connect({\n host: sftpCredentials.host,\n port: sftpCredentials.port,\n username: sftpCredentials.username,\n password: sftpCredentials.password\n}).then(res => sftp.list(\'/\'))\n.then(res => {\n const file = await sftp.get(\'/sample.png\');\n\n})\nRun Code Online (Sandbox Code Playgroud)\n\n我想将此文件保存在本地。文件是一个可读流,如下 -
\n\nReadStream {_readableState: ReadableState, readable: true, domain: null, _events: Object, _eventsCount: 3, \xe2\x80\xa6}\n _events:Object {end: , error: , readable: }\n _eventsCount:3\n _maxListeners:undefined\n _readableState:ReadableState {objectMode: false, highWaterMark: 65536, buffer: BufferList, \xe2\x80\xa6}\n autoClose:true\n destroyed:false\n domain:null\n end:undefined\n flags:"r"\n handle:Buffer(4) [0, 0, 0, …Run Code Online (Sandbox Code Playgroud) 一直在尝试在代码覆盖率徽章中显示覆盖率。我在 CI/CD --> JOBS 部分获得覆盖范围,但 gitlab 徽章中的覆盖范围没有更新。以下是我的 gitlab-ci.yml 文件,我在其中找到覆盖范围并将其保存在工件中。
variables:
DOCKER_DRIVER: overlay2
GIT_CLEAN_FLAGS: -ffdx -e node_modules/
GIT_DEPTH: 10
stages:
- test
test:
image: docker:19.03.12
services:
- docker:dind
stage: test
script:
- c8 --all --reporter=text-summary npm run test
artifacts:
when: always
paths:
- coverage
Run Code Online (Sandbox Code Playgroud)
配置-测试覆盖率解析-“Statements\s+:\s(\d+.?\d+)%”
我试图在将所有者保存在猫鼬之前调用预保存钩子。不调用预保存钩子。有什么办法吗?
const baseOptions = {
discriminatorKey: '__type',
collection: 'users'
}
const Base = mongoose.model('Base', new mongoose.Schema({}, baseOptions));
const Owner = Base.discriminator('Owner', new mongoose.Schema({
firstName: String,
email: String,
password: String,
}));
const Staff = Base.discriminator('Staff', new mongoose.Schema({
firstName: String,
}));
Run Code Online (Sandbox Code Playgroud)
这不叫
Owner.schema.pre('save', function (next) {
if (!!this.password) {
// ecryption of password
} else {
next();
}
})
Run Code Online (Sandbox Code Playgroud) javascript ×3
node.js ×3
arrays ×1
ecmascript-6 ×1
gitlab ×1
mongoose ×1
node-streams ×1
numbers ×1
ssh2 ×1
string ×1
unit-testing ×1