Доступ к Google Cloud Datastore из приложения Node, работающего на локальном компьютере

Преобразование входной строки в число, а не сохранение ее как строки, ограничивает решение максимально допустимым значением float / integer на этом компьютере / браузере. Мой сценарий ниже обрабатывает валюту до 1 триллиона долларов - 1 цент :-). Я могу быть расширен, чтобы обрабатывать до 999 триллионов, добавив 3 или 4 строки кода.

var ones = ["","One","Two","Three","Four","Five","Six","Seven","Eight",
            "Nine","Ten","Eleven","Twelve","Thirteen","Fourteen",
            "Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"]; 

var tens = ["","","Twenty","Thirty","Forty","Fifty","Sixty","Seventy",
            "Eighty","Ninety"]; 


function words999(n999)   {    // n999 is an integer less than or equal to 999.
//
// Accept any 3 digit int incl 000 & 999 and return words.
// 

    var words = ''; var Hn = 0; var n99 = 0;

    Hn = Math.floor(n999 / 100);                  // # of hundreds in it

    if (Hn > 0)   {                               // if at least one 100

      words = words99(Hn) + " Hundred";           // one call for hundreds
    }

    n99 = n999 - (Hn * 100);                      // subtract the hundreds.

    words += ((words == '')?'':' ') + words99(n99); // combine the hundreds with tens & ones.

    return words;
}                            // function words999( n999 )

function words99(n99)   {    // n99 is an integer less than or equal to 99.
//
// Accept any 2 digit int incl 00 & 99 and return words.
// 

    var words = ''; var Dn = 0; var Un = 0;

    Dn = Math.floor(n99 / 10);           // # of tens

    Un = n99 % 10;                       // units

    if (Dn > 0 || Un > 0) {

      if (Dn < 2) {

        words += ones[Dn * 10 + Un];     // words for a # < 20

      } else {

        words += tens[Dn];

        if (Un > 0) words += "-" + ones[Un];
      }
    }                               // if ( Dn > 0 || Un > 0 )

    return words;
}                                   // function words99( n99 )

function getAmtInWords(id1, id2) {  // use numeric value of id1 to populate text in id2 
//
// Read numeric amount field and convert into word amount
// 

    var t1 = document.getElementById(id1).value;

    var t2 = t1.trim();

    amtStr = t2.replace(/,/g,'');        // $123,456,789.12 = 123456789.12

    dotPos = amtStr.indexOf('.');        // position of dot before cents, -ve if it doesn't exist.

    if (dotPos > 0) {

      dollars = amtStr.slice(0,dotPos);  // 1234.56 = 1234
      cents   = amtStr.slice(dotPos+1);  // 1234.56 = .56

    } else if (dotPos == 0) {

      dollars = '0';
      cents   = amtStr.slice(dotPos+1);  // 1234.56 = .56

    } else {

      dollars = amtStr.slice(0);         // 1234 = 1234
      cents   = '0'; 
    }

    t1      = '000000000000' + dollars;  // to extend to trillion, use 15 zeros
    dollars =  t1.slice(-12);            // and -15 here.

    billions  = Number(dollars.substr(0,3));
    millions  = Number(dollars.substr(3,3));
    thousands = Number(dollars.substr(6,3));
    hundreds  = Number(dollars.substr(9,3));

    t1 = words999(billions);    bW = t1.trim();   // Billions  in words

    t1 = words999(millions);    mW = t1.trim();   // Millions  in words

    t1 = words999(thousands);   tW = t1.trim();   // Thousands in words

    t1 = words999(hundreds);    hW = t1.trim();   // Hundreds  in words

    t1 = words99(cents);        cW = t1.trim();   // Cents     in words

    var totAmt = '';

    if (bW != '')   totAmt += ((totAmt != '') ? ' '  : '') + bW + ' Billion';
    if (mW != '')   totAmt += ((totAmt != '') ? ' '  : '') + mW + ' Million';
    if (tW != '')   totAmt += ((totAmt != '') ? ' '  : '') + tW + ' Thousand';
    if (hW != '')   totAmt += ((totAmt != '') ? ' '  : '') + hW + ' Dollars';

    if (cW != '')   totAmt += ((totAmt != '') ? ' and ' : '') + cW + ' Cents';

//  alert('totAmt = ' + totAmt);    // display words in a alert

    t1 = document.getElementById(id2).value;

    t2 = t1.trim();

    if (t2 == '')  document.getElementById(id2).value = totAmt;

    return false;
}                        // function getAmtInWords( id1, id2 )

// ======================== [ End Code ] ====================================
0
задан 17 January 2019 в 06:43
поделиться

1 ответ

Google Cloud Datastore имеет локальный сервер разработки, который вы можете использовать: https://developers.google.com/datastore/docs/tools/devserver

Вы можете создать и запустить локальное хранилище данных с использованием инструмента gcd, с которым связан документ, приведенный выше.

Если вы используете DatastoreHelper.getDatastoreFromEnv(); для создания хранилища данных, вы можете указать ему подключиться к локальной базе данных, экспортировав переменную env DATASTORE_HOST:

export DATASTORE_HOST=http://localhost:8080
0
ответ дан Bira 17 January 2019 в 06:43
поделиться
Другие вопросы по тегам:

Похожие вопросы: