小编Kom*_*sal的帖子

对包含数字和字符串的数组进行排序

我试图将包含字符串,数字和数字的数组排序为字符串(例如'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)

javascript arrays string numbers ecmascript-6

18
推荐指数
3
解决办法
2580
查看次数

转换可读流并将其保存为本地文件

我正在使用 ssh2-sftp-client 从远程服务器获取文件。我正在将文件放入可读流中。我想将此可读流转换为所需的文件(sample.png 作为 png 文件,sample.doc 文件作为 doc 文件等)

\n\n

这是我的代码-

\n\n
let 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})\n
Run Code Online (Sandbox Code Playgroud)\n\n

我想将此文件保存在本地。文件是一个可读流,如下 -

\n\n
ReadStream {_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)

javascript node.js node-streams ssh2

8
推荐指数
1
解决办法
1万
查看次数

徽章中的覆盖范围“未知”

一直在尝试在代码覆盖率徽章中显示覆盖率。我在 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+)%”

在此输入图像描述

unit-testing code-coverage node.js gitlab test-coverage

6
推荐指数
0
解决办法
1954
查看次数

Mongoose 预保存未与鉴别器一起运行

我试图在将所有者保存在猫鼬之前调用预保存钩子。不调用预保存钩子。有什么办法吗?

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 mongoose node.js discriminator

5
推荐指数
1
解决办法
419
查看次数