2017-12-17 16 views
1

math.js의 디자인을 개선하려고합니다.npm 모듈의 디자인 향상

개념이 많다는, 나는 작은 것들로 큰 모듈을 분리하는 것을 시도하고있다 :

enter image description here

내가 lib/index.js에서 단지 하나의 수입을 가지고, 그것은 작동하는 것 같군 :

module.exports = require('./trigonometric') 

하지만 두 라이너가 (또는 그 이상)의 경우,

module.exports = require('./trigonometric') 
module.exports = require('./unit') 

아래의 오류와 같이 오류가 발생합니다.

모든 기능이 하나의 파일에 있었지만 엄청나게 커지는 것을 보았으므로 수학 기능의 여러 영역 (예 : 단위, 삼각법 등)간에 양호한 구분이 이루어지기로 결정했습니다.

This이 노력의 첫 번째 커밋입니다. 나는 수입을 확인할 수 없기 때문에 당신이 볼 수있는 나는, 거기에 붙어 :

Failures: 

    1) #math.js Testing ceil function 
    Message: 
    TypeError: math.ceil is not a function 
    Stacktrace: 
    TypeError: math.ceil is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:419) 

    2) #math.js Testing round function 
    Message: 
    TypeError: math.round is not a function 
    Stacktrace: 
    TypeError: math.round is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:755) 

    3) #math.js Testing fround function 
    Message: 
    TypeError: math.fround is not a function 
    Stacktrace: 
    TypeError: math.fround is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:1104) 

    4) #math.js Testing floor function 
    Message: 
    TypeError: math.floor is not a function 
    Stacktrace: 
    TypeError: math.floor is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:1469) 

    5) #math.js Testing random function 
    Message: 
    TypeError: math.random is not a function 
    Stacktrace: 
    TypeError: math.random is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:1817) 

    6) #math.js Testing sign function 
    Message: 
    TypeError: math.sign is not a function 
    Stacktrace: 
    TypeError: math.sign is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:2025) 

    7) #math.js Testing drop digit functions 
    Message: 
    TypeError: math.dropFirstDigit is not a function 
    Stacktrace: 
    TypeError: math.dropFirstDigit is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:2366) 

    8) #math.js Testing format function 
    Message: 
    TypeError: math.format is not a function 
    Stacktrace: 
    TypeError: math.format is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:3022) 

    9) #math.js Testing abs function 
    Message: 
    TypeError: math.abs is not a function 
    Stacktrace: 
    TypeError: math.abs is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:3389) 

    10) #math.js Testing array functions 
    Message: 
    TypeError: math.randomElement is not a function 
    Stacktrace: 
    TypeError: math.randomElement is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:3753) 

    11) #math.js Testing pow function 
    Message: 
    TypeError: math.pow is not a function 
    Stacktrace: 
    TypeError: math.pow is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:4236) 

    12) #math.js Testing square function 
    Message: 
    TypeError: math.square is not a function 
    Stacktrace: 
    TypeError: math.square is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:4502) 

    13) #math.js Testing imul function 
    Message: 
    TypeError: math.imul is not a function 
    Stacktrace: 
    TypeError: math.imul is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:4770) 

    14) #math.js Testing cube function 
    Message: 
    TypeError: math.cube is not a function 
    Stacktrace: 
    TypeError: math.cube is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:5037) 

    15) #math.js Testing sqrt function 
    Message: 
    TypeError: math.sqrt is not a function 
    Stacktrace: 
    TypeError: math.sqrt is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:5299) 

    16) #math.js Testing cbrt function 
    Message: 
    TypeError: math.cbrt is not a function 
    Stacktrace: 
    TypeError: math.cbrt is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:5561) 

    17) #math.js Testing exp function 
    Message: 
    TypeError: math.exp is not a function 
    Stacktrace: 
    TypeError: math.exp is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:5825) 

    18) #math.js Testing exp function 
    Message: 
    TypeError: math.expm1 is not a function 
    Stacktrace: 
    TypeError: math.expm1 is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:6099) 

    19) #math.js Testing between function 
    Message: 
    TypeError: math.between is not a function 
    Stacktrace: 
    TypeError: math.between is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:6381) 

    20) #math.js Testing trunv function 
    Message: 
    TypeError: math.trunc is not a function 
    Stacktrace: 
    TypeError: math.trunc is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:6743) 

    21) #math.js Testing temperature functions 
    Message: 
    TypeError: math.toFahrenheit is not a function 
    Stacktrace: 
    TypeError: math.toFahrenheit is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:7097) 

    22) #math.js Testing hypot function 
    Message: 
    TypeError: math.hypot is not a function 
    Stacktrace: 
    TypeError: math.hypot is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:7380) 

    23) #math.js Testing greatestCommonDivisor function 
    Message: 
    TypeError: math.greatestCommonDivisor is not a function 
    Stacktrace: 
    TypeError: math.greatestCommonDivisor is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:7756) 

    24) #math.js Testing factorial function 
    Message: 
    TypeError: math.factorial is not a function 
    Stacktrace: 
    TypeError: math.factorial is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:8063) 

    25) #math.js Testing prime function 
    Message: 
    TypeError: math.isPrime is not a function 
    Stacktrace: 
    TypeError: math.isPrime is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:8491) 

    26) #math.js Testing even function 
    Message: 
    TypeError: math.isEven is not a function 
    Stacktrace: 
    TypeError: math.isEven is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:8763) 

    27) #math.js Testing odd function 
    Message: 
    TypeError: math.isOdd is not a function 
    Stacktrace: 
    TypeError: math.isOdd is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:9033) 

    28) #math.js Common log tests 
    Message: 
    TypeError: math.log is not a function 
    Stacktrace: 
    TypeError: math.log is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:9298) 

    29) #math.js Common log2 tests 
    Message: 
    TypeError: math.log2 is not a function 
    Stacktrace: 
    TypeError: math.log2 is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:9727) 

    30) #math.js Common log10 tests 
    Message: 
    TypeError: math.log10 is not a function 
    Stacktrace: 
    TypeError: math.log10 is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:10308) 

    31) #math.js Common log1p tests 
    Message: 
    TypeError: math.log1p is not a function 
    Stacktrace: 
    TypeError: math.log1p is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:10824) 

    32) #math.js Testing unit converters 
    Message: 
    TypeError: math.feetToInches is not a function 
    Stacktrace: 
    TypeError: math.feetToInches is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:11188) 

    33) #math.js Testing trigonometric functions 
    Message: 
    TypeError: math.sin is not a function 
    Stacktrace: 
    TypeError: math.sin is not a function 
    at jasmine.Spec.<anonymous> (/Users/toubou91/git/math.js/spec/math-spec.js:9:12715) 

Finished in 0.024 seconds 
33 tests, 33 assertions, 33 fai 

나는 두 디렉토리에 같은 이름 (하는 index.js)를 사용하고 있기 때문에 될 수 있다고 의심 (삼각 단위) , 그래도 나는 그것을 작동하게 만들 수 없다.

아이디어가 있으십니까?

답변

2

module.exports이 대상입니다. 당신이 할 때

module.exports = require('./trigonometric'); 
module.exports = require('./unit'); 

당신은 먼저 그것을 삼각 모듈에 할당하고, 그것을 단위 모듈로 덮어 씁니다. 우리가 수학 개체를 CONSOLE.LOG 경우 우리가 얻을 :

{ yardsToFeet: [Function: yardsToFeet], 
    feetToYards: [Function: feetToYards], 
    ... 
    yardsToMiles: [Function: yardsToMiles], 
    yardsToMeters: [Function: yardsToMeters] } 

우리는 삼각 함수는 테스트가 실패하는 이유입니다, 누락 된 것을 볼 수 있습니다. dev에 지점에서

당신도했다 :

module.exports = [ 
    require('./trigonometric'), 
    require('./unit') 
]; 

이가이처럼 사용할 필요가 배열한다 :

console.log(mathjs[0].sin(0.1)); 

또는 우리가 CONSOLE.LOG 경우 수학 개체 :

[ { sin: [Function: sin], 
    cos: [Function: cos], 
    ... 
    log10: [Function: log10], 
    log1p: [Function: log1p] }, 
    { yardsToFeet: [Function: yardsToFeet], 
    feetToYards: [Function: feetToYards], 
    yardsToInches: [Function: yardsToInches], 
    ... 
    yardsToMiles: [Function: yardsToMiles], 
    yardsToMeters: [Function: yardsToMeters] } ] 

매우 사용자 친화적이지 않습니다.


대신 당신은 일반적인 객체에 함께 두 개의 모듈을 병합 할 수 있습니다 후 해당 객체 수출 : console.log(mathjs.sin(0.1));

또한 두 가지를 내보낼 수 있습니다

const trig = require('./trigonometric'); 
const unit = require('./unit'); 

module.exports = Object.assign({}, trig, unit); 

그것은 다음과 같이 사용할 수있다 삼각 함수 및 삼각형이라는 하위 객체 :

module.exports.trigonometric = require('./trigonometric'); 
module.exports.unit = require('./unit'); 

이런 식으로 사용했습니다 : console.log(mathjs.trigonometric.sin(0.1));

+0

와우, 감사합니다. @MikaS! 더 나은 디자인 선택으로 무엇을 제안 하시겠습니까? 내 테스트를 더 깨끗하게 유지하기 때문에 첫 번째 옵션으로 갈 것입니다. – thodorisbais

+1

@ 처음에는 생각했습니다. mathjs 객체에 하위 객체가 너무 길어집니다. 너무 많은 함수가있는 지점에 도달하면 중요하지 않은 함수를 하위 객체에 넣을 수 있습니다. – MikaS

+0

어떨까요? module.exports = Object.assign ( require ('./ unit'), require ('./ trigonometric') ); – thodorisbais