<commit_before>/* Copyright(c) 2016 <NAME>. */
/* jshint strict: true, esversion: 6 */

const amazon_order_history_order = (function() {
    'use strict';

    class OrderTracker  {
        constructor() {
            self.promises_by_id = {};
            self.pending_ids = new Set();
        }

        constructorStarted(order_object) {
        }

        idKnown(id) {
        }

        detailPromiseResolved(id) {
        }

        paymentsPromiseResolved(id) {
        }
    }

    const order_tracker = new OrderTracker();

    function getField(xpath, doc, elem) {
        const valueElem = amazon_order_history_util.findSingleNodeValue(xpath, doc, elem);
        try {
            return valueElem.textContent.trim();
        } catch (_) {
            return '?';
        }
    }

    class Order {
        constructor(ordersPageElem, request_scheduler) {
            this.id = null;
            order_tracker.constructorStarted(this);
            this.date = null;
            this.total = null;
            this.who = null;
            this.detail_promise = null;
            this.items = null;
            this.request_scheduler = request_scheduler;
            this.extractOrder(ordersPageElem);
        }

        extractOrder(elem) {
            const getItems = function(elem) {
                /*
                  <a class="a-link-normal" href="/gp/product/B01NAE8AW4/ref=oh_aui_d_detailpage_o01_?ie=UTF8&amp;psc=1">
                      The Rise and Fall of D.O.D.O.
                  </a>
                  or
                  <a class="a-link-normal" href="/gp/product/B06X9BZNDM/ref=oh_aui_d_detailpage_o00_?ie=UTF8&amp;psc=1">
                      Provenance
                  </a>
                  but a-link-normal is more common than this, so we need to match on gp/product
                  like this: .//div[@class="a-row"]/a[@class="a-link-normal"][contains(@href,"/gp/product/")]
                  then we get:
                      name from contained text
                      link from href attribute
                      item: not sure what we use this for - will it still work?
                */
                const itemResult = amazon_order_history_util.findMultipleNodeValues(
                    './/div[@class="a-row"]/a[@class="a-link-normal"][contains(@href,"/gp/product/")]',
                    elem.ownerDocument,
                    elem);
                const items = {};
                itemResult.forEach(
                    function(item){
                        const name = item.innerText.replace(/[\n\r]/g, " ")
                                                 .replace(/  */g, " ")
                                                 .trim();
                        const link = item.getAttribute('href');
                        items[name] = link;
                    }
                );
                return items;
            };
            const doc = elem.ownerDocument;
            this.date = getField(
                ['Commande effectuée', 'Order placed'].map(
                    label => sprintf(
                        './/div[contains(span,"%s")]' +
                        '/../div/span[contains(@class,"value")]',
                        label
                    )
                ).join('|'),
                doc,
                elem
            );
            this.total = getField('.//div[contains(span,"Total")]' +
                '/../div/span[contains(@class,"value")]', doc, elem);
            this.who = getField('.//div[contains(@class,"recipient")]' +
                '//span[@class="trigger-text"]', doc, elem);
            if (this.who === '?') {
                this.who = 'N/A';
            }
            this.id = getField(
                ['Order #', 'commande'].map(
                    label => sprintf(
                        './/div[contains(@class,"a-row")]' +
                        '[span[contains(@class,"label")]]' +
                        '[span[contains(@class,"value")]]' +
                        '[contains(span,"%s")]' +
                        '/span[contains(@class,"value")]',
                        label
                    )
                ).join(' | '),
                doc,
                elem
            );
            order_tracker.idKnown(this.id);
            this.items = getItems(elem);
            this.detail_promise = new Promise(
                function(resolve, reject) {
                    const query = amazon_order_history_util.getOrderDetailUrl(this.id);
                    const event_converter = function(evt) {
                        const parser = new DOMParser();
                        const doc = parser.parseFromString(
                            evt.target.responseText, 'text/html'
                        );
                        const gift = function(){
                            let a = getField(
                                '//div[contains(@id,"od-subtotals")]//' +
                                'span[contains(text(),"Gift")]/' +
                                    'parent::div/following-sibling::div/span',
                                doc,
                                doc.documentElement
                            );
                            let b;
                            if( a !== '?') {
                                return a.replace('-', '');
                            }
                            a = getField(
                                '//*[text()[contains(.,"Gift Certificate")]]',
                                doc,
                                doc.documentElement
                            );
                            if( a !== null ) {
                                b = a.match(
                                    /Gift Certificate.Card Amount: *([$£€0-9.]*)/);
                                if( b !== null ) {
                                    return b[1];
                                }
                            }
                            a = getField(
                                '//*[text()[contains(.,"Gift Card")]]',
                                doc,
                                doc.documentElement
                            );
                            if( a !== null ) {
                                b = a.match(
                                    /Gift Card Amount: *([$£€0-9.]*)/);
                                if( b !== null ) {
                                    return b[1];
                                }
                            }
                            return 'N/A';
                        }.bind(this);
                        const postage = function() {
                            let a = getField(
                                ['Postage', 'Shipping', 'Livraison'].map(
                                    label => sprintf(
                                        '//div[contains(@id,"od-subtotals")]//' +
                                        'span[contains(text(),"%s")]/' +
                                        'parent::div/following-sibling::div/span',
                                        label
                                    )
                                ).join('|'),
                                doc,
                                doc.documentElement
                            );
                            if (a !== '?') {
                                return a;
                            }
                            return 'N/A';
                        }.bind(this);
                        const vat = function(){
                            let a = getField(
                                ['VAT', 'tax', 'TVA'].map(
                                    label => sprintf(
                                        '//div[contains(@id,"od-subtotals")]//' +
                                        'span[contains(text(),"%s") and not(contains(.,"Before"))]/' +
                                        'parent::div/following-sibling::div/span',
                                        label
                                    )
                                ).join('|'),
                                doc,
                                doc.documentElement<commit_msg>include amazon pantry delivery costs in postage<commit_after>/* Copyright(c) 2016 <NAME>. */
/* jshint strict: true, esversion: 6 */

const amazon_order_history_order = (function() {
    'use strict';

    class OrderTracker  {
        constructor() {
            self.promises_by_id = {};
            self.pending_ids = new Set();
        }

        constructorStarted(order_object) {
        }

        idKnown(id) {
        }

        detailPromiseResolved(id) {
        }

        paymentsPromiseResolved(id) {
        }
    }

    const order_tracker = new OrderTracker();

    function getField(xpath, doc, elem) {
        const valueElem = amazon_order_history_util.findSingleNodeValue(xpath, doc, elem);
        try {
            return valueElem.textContent.trim();
        } catch (_) {
            return '?';
        }
    }

    class Order {
        constructor(ordersPageElem, request_scheduler) {
            this.id = null;
            order_tracker.constructorStarted(this);
            this.date = null;
            this.total = null;
            this.who = null;
            this.detail_promise = null;
            this.items = null;
            this.request_scheduler = request_scheduler;
            this.extractOrder(ordersPageElem);
        }

        extractOrder(elem) {
            const getItems = function(elem) {
                /*
                  <a class="a-link-normal" href="/gp/product/B01NAE8AW4/ref=oh_aui_d_detailpage_o01_?ie=UTF8&amp;psc=1">
                      The Rise and Fall of D.O.D.O.
                  </a>
                  or
                  <a class="a-link-normal" href="/gp/product/B06X9BZNDM/ref=oh_aui_d_detailpage_o00_?ie=UTF8&amp;psc=1">
                      Provenance
                  </a>
                  but a-link-normal is more common than this, so we need to match on gp/product
                  like this: .//div[@class="a-row"]/a[@class="a-link-normal"][contains(@href,"/gp/product/")]
                  then we get:
                      name from contained text
                      link from href attribute
                      item: not sure what we use this for - will it still work?
                */
                const itemResult = amazon_order_history_util.findMultipleNodeValues(
                    './/div[@class="a-row"]/a[@class="a-link-normal"][contains(@href,"/gp/product/")]',
                    elem.ownerDocument,
                    elem);
                const items = {};
                itemResult.forEach(
                    function(item){
                        const name = item.innerText.replace(/[\n\r]/g, " ")
                                                 .replace(/  */g, " ")
                                                 .trim();
                        const link = item.getAttribute('href');
                        items[name] = link;
                    }
                );
                return items;
            };
            const doc = elem.ownerDocument;
            this.date = getField(
                ['Commande effectuée', 'Order placed'].map(
                    label => sprintf(
                        './/div[contains(span,"%s")]' +
                        '/../div/span[contains(@class,"value")]',
                        label
                    )
                ).join('|'),
                doc,
                elem
            );
            this.total = getField('.//div[contains(span,"Total")]' +
                '/../div/span[contains(@class,"value")]', doc, elem);
            this.who = getField('.//div[contains(@class,"recipient")]' +
                '//span[@class="trigger-text"]', doc, elem);
            if (this.who === '?') {
                this.who = 'N/A';
            }
            this.id = getField(
                ['Order #', 'commande'].map(
                    label => sprintf(
                        './/div[contains(@class,"a-row")]' +
                        '[span[contains(@class,"label")]]' +
                        '[span[contains(@class,"value")]]' +
                        '[contains(span,"%s")]' +
                        '/span[contains(@class,"value")]',
                        label
                    )
                ).join(' | '),
                doc,
                elem
            );
            order_tracker.idKnown(this.id);
            this.items = getItems(elem);
            this.detail_promise = new Promise(
                function(resolve, reject) {
                    const query = amazon_order_history_util.getOrderDetailUrl(this.id);
                    const event_converter = function(evt) {
                        const parser = new DOMParser();
                        const doc = parser.parseFromString(
                            evt.target.responseText, 'text/html'
                        );
                        const gift = function(){
                            let a = getField(
                                '//div[contains(@id,"od-subtotals")]//' +
                                'span[contains(text(),"Gift")]/' +
                                    'parent::div/following-sibling::div/span',
                                doc,
                                doc.documentElement
                            );
                            let b;
                            if( a !== '?') {
                                return a.replace('-', '');
                            }
                            a = getField(
                                '//*[text()[contains(.,"Gift Certificate")]]',
                                doc,
                                doc.documentElement
                            );
                            if( a !== null ) {
                                b = a.match(
                                    /Gift Certificate.Card Amount: *([$£€0-9.]*)/);
                                if( b !== null ) {
                                    return b[1];
                                }
                            }
                            a = getField(
                                '//*[text()[contains(.,"Gift Card")]]',
                                doc,
                                doc.documentElement
                            );
                            if( a !== null ) {
                                b = a.match(
                                    /Gift Card Amount: *([$£€0-9.]*)/);
                                if( b !== null ) {
                                    return b[1];
                                }
                            }
                            return 'N/A';
                        }.bind(this);
                        const postage = function() {
                            let a = getField(
                                ['Postage', 'Shipping', 'Livraison', 'Delivery'].map(
                                    label => sprintf(
                                        '//div[contains(@id,"od-subtotals")]//' +
                                        'span[contains(text(),"%s")]/' +
                                        'parent::div/following-sibling::div/span',
                                        label
                                    )
                                ).join('|'),
                                doc,
                                doc.documentElement
                            );
                            if (a !== '?') {
                                return a;
                            }
                            return 'N/A';
                        }.bind(this);
                        const vat = function(){
                            let a = getField(
                                ['VAT', 'tax', 'TVA'].map(
                                    label => sprintf(
                                        '//div[contains(@id,"od-subtotals")]//' +
                                        'span[contains(text(),"%s") and not(contains(.,"Before"))]/' +
                                        'parent::div/following-sibling::div/span',
                                        label
                                    )
                                ).join('|'),
                                doc,
                                doc.documentElement
{'fraction_non_alphanumeric': 0.11805049769361496, 'fraction_numerical': 0.0031561058509346927, 'mean_word_length': 0.8015525912967417, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 56, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1575260', 'n_tokens_mistral': 3938, 'n_tokens_neox': 3703, 'n_words': 646}