<commit_before>function $_global_sputils_rest () {
  (function (window, $, _spPageContextInfo) {
    'use strict';

    /*

      SETUP

    */

    // This is used to cache results
    // from grabbing the request digest.
    var requestDigest;

    // Simple AJAX request for fetching the request digest
    // from /_api/contextinfo. This is used as a fallback
    // for the one embedded in the SharePoint page.

    // Returns a promise resolving to the digest string.
    var requestFormDigest = function () {
      var deferred = $.Deferred();

      get("/_api/contextinfo", { "type": "POST" })
        .then(function (data) {
          deferred.resolve(data.d.GetContextWebInformation.FormDigestValue);
        });

      return deferred.promise();
    };

    // Utility for grabbing the digest off the page in
    // an asynchronous manner. Solves the issue of script
    // running before page has loaded proper.

    // Returns a promise resolving to the digest string.
    var withRequestDigest = function () {
      var deferred = $.Deferred();

      if (requestDigest) {
        deferred.resolve(requestDigest);
      }
      else {
        var rd = $("#__REQUESTDIGEST");
        if (rd.length > 0 && rd.val() !== "InvalidFormDigest") {
          requestDigest = rd.val();
          deferred.resolve(rd.val());
        }
        else {
          requestFormDigest()
            .then(function (digest) {
              requestDigest = digest;
              deferred.resolve(digest);
            });
        }
      }

      return deferred.promise();
    };

    // Gets the default config object for ajax requests.
    // Is asynchronous for consistency.

    // Returns a promise resolving to the ajax defaults.
    var getDefaults = function (url, config) {
      var deferred = $.Deferred();

      var defaults = {
        // If the URL is not absolute, get the missing part
        // from _spPageContextInfo
        url: url.indexOf('http') > -1 ? url : _spPageContextInfo.webAbsoluteUrl + url,
        type: "GET",
        headers: {
          "accept": "application/json;odata=verbose"
        }
      };

      $.extend(defaults, config);
      deferred.resolve(defaults);
      return deferred.promise();
    };

    // Gets the default config object for ajax post requests,
    // which includes the getConfig and the request digest.

    // Returns a promise resolving to the ajax post defaults.
    var postDefaults = function (url, data, config) {
      return $.when(getDefaults(url), withRequestDigest())
        .then(function (defaults, digest) {
          var added = {
            type: "POST",
            data: data,
            contentType: "application/json;odata=verbose",
            headers: {
              "X-RequestDigest": digest,
            }
          };

          return $.extend(true, defaults, added, config);
        });
    };

    /*

      API

    */

    var get = function (url, config) {
      return getDefaults(url, config)
        .then(function (defaults) {
          return $.ajax(defaults);
        });
    };

    /*

    EXAMPLE USE:

    sputils.rest.get("/_api/web/lists").then(function (data) {
      $.each(data.d.results, function (idx,el) {
        console.log(el);
      });
    });

    */

    var post = function (url, data, config) {
      data = typeof data === 'string' ? data : JSON.stringify(data);
      return postDefaults(url, data, config).then(function (defaults) {
        return $.ajax(defaults);
      });
    };

    /*

    EXAMPLE USE

    var data = {"Title":"REST API FTW",
                "__metadata": { "type": "SP.Data.AnnouncementsListItem"}};

    sputils.rest.post("/_api/Web/Lists/getByTitle('Announcements')/items/", data)
      .then(function (data) { console.log(data) });

    */

    /*

      SPECIALIZED

    */

    var getListByName = function (name, config) {
      var url = '/_api/Web/Lists/getByTitle(\'' + name + '\')/items/';
      return get(url, config);
    };

    /*

    EXAMPLE USE

    sputils.rest.getListByName('Announcements')
      .then(function (data) { console.log(data.d.results) });

    */

    var postListByName = function (name, data, config) {
      var url = '/_api/Web/Lists/getByTitle(\'' + name + '\')/items/';
      return post(url, data, config);
    };

    /*

    EXAMPLE USE

    var data = {"Title": "RESTlessly POSTing",
                "__metadata": { type: "SP.Data.AnnouncementsListItem"} };
    sputils.rest.postListByName("Announcements", data)
      .then(function (data) { console.log(data); });

    */

    window.sputils = window.sputils || {};
    window.sputils.rest = {
      get: get,
      post: post,
      getListByName: getListByName,
      postListByName: postListByName
    };
  })(window, jQuery, _spPageContextInfo);
}
$_global_sputils_rest();<commit_msg>Adds unwrapResults to rest API<commit_after>function $_global_sputils_rest () {
  (function (window, $, _spPageContextInfo) {
    'use strict';

    /*

      SETUP

    */

    // This is used to cache results
    // from grabbing the request digest.
    var requestDigest;

    // Simple AJAX request for fetching the request digest
    // from /_api/contextinfo. This is used as a fallback
    // for the one embedded in the SharePoint page.

    // Returns a promise resolving to the digest string.
    var requestFormDigest = function () {
      var deferred = $.Deferred();

      get("/_api/contextinfo", { "type": "POST" })
        .then(function (data) {
          deferred.resolve(data.d.GetContextWebInformation.FormDigestValue);
        });

      return deferred.promise();
    };

    // Utility for grabbing the digest off the page in
    // an asynchronous manner. Solves the issue of script
    // running before page has loaded proper.

    // Returns a promise resolving to the digest string.
    var withRequestDigest = function () {
      var deferred = $.Deferred();

      if (requestDigest) {
        deferred.resolve(requestDigest);
      }
      else {
        var rd = $("#__REQUESTDIGEST");
        if (rd.length > 0 && rd.val() !== "InvalidFormDigest") {
          requestDigest = rd.val();
          deferred.resolve(rd.val());
        }
        else {
          requestFormDigest()
            .then(function (digest) {
              requestDigest = digest;
              deferred.resolve(digest);
            });
        }
      }

      return deferred.promise();
    };

    // Gets the default config object for ajax requests.
    // Is asynchronous for consistency.

    // Returns a promise resolving to the ajax defaults.
    var getDefaults = function (url, config) {
      var deferred = $.Deferred();

      var defaults = {
        // If the URL is not absolute, get the missing part
        // from _spPageContextInfo
        url: url.indexOf('http') > -1 ? url : _spPageContextInfo.webAbsoluteUrl + url,
        type: "GET",
        headers: {
          "accept": "application/json;odata=verbose"
        }
      };

      $.extend(defaults, config);
      deferred.resolve(defaults);
      return deferred.promise();
    };

    // Gets the default config object for ajax post requests,
    // which includes the getConfig and the request digest.

    // Returns a promise resolving to the ajax post defaults.
    var postDefaults = function (url, data, config) {
      return $.when(getDefaults(url), withRequestDigest())
        .then(function (defaults, digest) {
          var added = {
            type: "POST",
            data: data,
            contentType: "application/json;odata=verbose",
            headers: {
              "X-RequestDigest": digest,
            }
          };

          return $.extend(true, defaults, added, config);
        });
    };

    /*

      API

    */

    var get = function (url, config) {
      return getDefaults(url, config)
        .then(function (defaults) {
          return $.ajax(defaults);
        });
    };

    /*

    EXAMPLE USE:

    sputils.rest.get("/_api/web/lists").then(function (data) {
      $.each(data.d.results, function (idx,el) {
        console.log(el);
      });
    });

    */

    var post = function (url, data, config) {
      data = typeof data === 'string' ? data : JSON.stringify(data);
      return postDefaults(url, data, config).then(function (defaults) {
        return $.ajax(defaults);
      });
    };

    /*

    EXAMPLE USE

    var data = {"Title":"REST API FTW",
                "__metadata": { "type": "SP.Data.AnnouncementsListItem"}};

    sputils.rest.post("/_api/Web/Lists/getByTitle('Announcements')/items/", data)
      .then(function (data) { console.log(data) });

    */

    // Results from the standard SharePoint REST APIs come
    // wrapped in objects. This convenience function unwraps
    // them for you. See example use.
    var unwrapResults = function (object) {
      return object.d.results;
    };

    /*

    EXAMPLE USE:

    sputils.rest.get("/_api/web/lists")
      .then(sputils.rest.unwrapResults)
      .then(function (data) {
        $.each(data, function (idx,el) {
          console.log(el);
        });
    });

    */

    /*

      SPECIALIZED

    */

    var getListByName = function (name, config) {
      var url = '/_api/Web/Lists/getByTitle(\'' + name + '\')/items/';
      return get(url, config)
        .then(unwrapResults);
    };

    /*

    EXAMPLE USE

    sputils.rest.getListByName('Announcements')
      .then(function (data) { console.log(data.d.results) });

    */

    var postListByName = function (name, data, config) {
      var url = '/_api/Web/Lists/getByTitle(\'' + name + '\')/items/';
      return post(url, data, config)
        .then(unwrapResults);
    };

    /*

    EXAMPLE USE

    var data = {"Title": "RESTlessly POSTing",
                "__metadata": { type: "SP.Data.AnnouncementsListItem"} };
    sputils.rest.postListByName("Announcements", data)
      .then(function (data) { console.log(data); });

    */

    window.sputils = window.sputils || {};
    window.sputils.rest = {
      get: get,
      post: post,
      getListByName: getListByName,
      postListByName: postListByName,
      unwrapResults: unwrapResults
    };
  })(window, jQuery, _spPageContextInfo);
}
$_global_sputils_rest();
{'fraction_non_alphanumeric': 0.13166877986930656, 'fraction_numerical': 0.00039012971813127864, 'mean_word_length': 2.067304816033503, 'pattern_counts': {'":': 16, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6232042', 'n_tokens_mistral': 3104, 'n_tokens_neox': 2921, 'n_words': 764}