當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript types.isString函數代碼示例

本文整理匯總了TypeScript中utils/types.isString函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript isString函數的具體用法?TypeScript isString怎麽用?TypeScript isString使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了isString函數的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: Promise

    return new Promise((resolve, reject) => {
      try {
        let MediaPlayer = android.media.MediaPlayer;
        let audioPath;

        let fileName = isString(options.audioFile) ? options.audioFile.trim() : "";
        if (fileName.indexOf("~/") === 0) {
          fileName = fs.path.join(fs.knownFolders.currentApp().path, fileName.replace("~/", ""));
          console.log('fileName: ' + fileName);
          audioPath = fileName;
        }

        this.player = new MediaPlayer();
          
        this.player.setAudioStreamType(android.media.AudioManager.STREAM_MUSIC);         
        this.player.setDataSource(audioPath);
        this.player.prepareAsync();

        // On Complete
        this.player.setOnCompletionListener(new MediaPlayer.OnCompletionListener({
          onCompletion: (mp) => {
            options.completeCallback();
          }
        }));

        // On Error
        this.player.setOnErrorListener(new MediaPlayer.OnErrorListener({
          onError: (mp: any, what: number, extra: number) => {
            options.errorCallback();
          }
        }));

        // On Info
        this.player.setOnInfoListener(new MediaPlayer.OnInfoListener({
          onInfo: (mp: any, what: number, extra: number) => {
            options.infoCallback();
          }
        }))

        // On Prepared
        this.player.setOnPreparedListener(new MediaPlayer.OnPreparedListener({
          onPrepared: (mp) => {
            mp.start();
            resolve();
          }
        }));

      } catch (ex) {
        reject(ex);
      }
    });
開發者ID:dwilanang,項目名稱:nativescript-audio,代碼行數:51,代碼來源:player.ts

示例2: validateArgs

function validateArgs(source: observable.Observable, eventName: string, handler: (eventData: observable.EventData) => void, target: any) {
    if (types.isNullOrUndefined(source)) {
        throw new Error("source is null or undefined");
    }

    if (types.isNullOrUndefined(target)) {
        throw new Error("target is null or undefined");
    }

    if (!types.isString(eventName)) {
        throw new Error("eventName is not a string");
    }

    if (!types.isFunction(handler)) {
        throw new Error("handler is not a function");
    }
}
開發者ID:329379172,項目名稱:NativeScript,代碼行數:17,代碼來源:weak-event-listener.ts

示例3: Error

    return new Promise<http.HttpResponse>((resolve, reject) => {

        try {
            var sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration();
            var queue = NSOperationQueue.mainQueue();
            var session = NSURLSession.sessionWithConfigurationDelegateDelegateQueue(
                sessionConfig, null, queue);

            var urlRequest = NSMutableURLRequest.requestWithURL(
                NSURL.URLWithString(options.url));

            urlRequest.HTTPMethod = types.isDefined(options.method) ? options.method : GET;

            urlRequest.setValueForHTTPHeaderField(USER_AGENT, USER_AGENT_HEADER);

            if (options.headers) {
                for (var header in options.headers) {
                    urlRequest.setValueForHTTPHeaderField(options.headers[header] + "", header);
                }
            }

            if (types.isString(options.content) || options.content instanceof FormData) {
                urlRequest.HTTPBody = NSString.alloc().initWithString(options.content.toString()).dataUsingEncoding(4);
            }

            if (types.isNumber(options.timeout)) {
                urlRequest.timeoutInterval = options.timeout / 1000;
            }

            var dataTask = session.dataTaskWithRequestCompletionHandler(urlRequest,
                function (data: NSData, response: NSHTTPURLResponse, error: NSError) {
                    if (error) {
                        reject(new Error(error.localizedDescription));
                    } else {
                        var headers = {};
                        if (response && response.allHeaderFields) {
                            var headerFields = response.allHeaderFields;
                            var keys = headerFields.allKeys;

                            for (var i = 0, l = keys.count; i < l; i++) {
                                var key = keys.objectAtIndex(i);
                                headers[key] = headerFields.valueForKey(key);
                            }
                        }

                        resolve({
                            content: {
                                raw: data,
                                toString: () => { return NSDataToString(data); },
                                toJSON: () => {
                                    ensureUtils();
                                    return utils.parseJSON(NSDataToString(data));
                                },
                                toImage: () => {
                                    ensureImageSource();
                                    if (UIImage.imageWithData["async"]) {
                                        return UIImage.imageWithData["async"](UIImage, [data])
                                                      .then(image => {
                                                          if (!image) {
                                                              throw new Error("Response content may not be converted to an Image");
                                                          }
                                    
                                                          var source = new imageSource.ImageSource();
                                                          source.setNativeSource(image);
                                                          return source;
                                                      });
                                    }
   
                                    return new Promise<any>((resolveImage, rejectImage) => {
                                        var img = imageSource.fromData(data);
                                        if (img instanceof imageSource.ImageSource) {
                                            resolveImage(img);
                                        } else {
                                            rejectImage(new Error("Response content may not be converted to an Image"));
                                        }

                                    });
                                },
                                toFile: (destinationFilePath?: string) => {
                                    var fs: typeof fsModule = require("file-system");
                                    var fileName = options.url;
                                    if (!destinationFilePath) {
                                        destinationFilePath = fs.path.join(fs.knownFolders.documents().path, fileName.substring(fileName.lastIndexOf('/') + 1));
                                    }
                                    if (data instanceof NSData) {
                                        data.writeToFileAtomically(destinationFilePath, true);
                                        return fs.File.fromPath(destinationFilePath);
                                    } else {
                                        reject(new Error(`Cannot save file with path: ${destinationFilePath}.`));
                                    }
                                }
                            },
                            statusCode: response.statusCode,
                            headers: headers
                        });
                    }
                });

            dataTask.resume();
        } catch (ex) {
//.........這裏部分代碼省略.........
開發者ID:Prettycode-KC,項目名稱:NativeScript,代碼行數:101,代碼來源:http-request.ios.ts


注:本文中的utils/types.isString函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。